# Dark horse Java notes Lecture 2 - Java Foundation

🎨 Personal introduction

👉 Hello, I'm wang Zai, the knowledge Porter

👉 Seriously share technology and record the learning process. If my sharing can help you, please support me 🍻

👉 Your support is my motivation to update every day.

👉 Likes: 👍 Leaving a message. ✍ Collection: ⭐

👉 Personal motto: the best choice for you and me is to implement the idea step by step.

# 1. Arithmetic operator

## 1.1 understanding of arithmetic operators

### 1.1.1 operators and expressions

Operator: a symbol that operates on a constant or variable

Expression: a constant or variable is connected by an operator, and it can be called an expression if it conforms to the java syntax

Expressions connected by different operators reflect different types of expressions

for instance:

```int a = 10;
int b =20;
int c = a + b;
```

+: is an operator and is an arithmetic operator

a + b: is an expression. Since + is an arithmetic operator, this expression is called an arithmetic expression

### 1.1.2 expression

Symboleffect
+plus
-reduce
*ride
/except
%Surplus

be careful:

1. /The difference between% and%: divide the two data, / get the quotient of the result,% get the remainder of the result
2. Integer operation can only get integers. To get decimals, floating-point numbers must be involved in the operation

### 1.1.3 "+" operation of string

1) char type participates in arithmetic operation and uses the decimal value corresponding to the computer bottom. We need to remember the value corresponding to three characters:

'a' - 97, a-z is continuous

'a' - 65 (A-Z is continuous)

'0' - 48 0-9 is continuous

2) When the arithmetic expression contains values of different basic data types, the type of the entire arithmetic expression will be automatically promoted

3) Promotion rules:

1. byte type, short type and char type will be promoted to int type, regardless of whether there are other types participating in the operation.
2. The type of the entire expression is automatically promoted to the same type as the highest level operand in the expression
3. Level order: byte,short,char - > int - > long - > float - > double

tips: because of the above reasons, we rarely use byte or short type to define integers in program development, and rarely use char type to define strings. Instead, we use string type, let alone char type for arithmetic operations

### 1.1.4 "+" operation of string

1) When a string appears in the "+" operation, the "+" is a string connector, not an operator

```System.out.println("itheima"+666)
```

2) In the "+" operation, if a string appears, it is a concatenation operator; otherwise, it is an arithmetic operator.

3) When the "+" operation is performed continuously, it is executed one by one from left to right. Only when the string is encountered, "+" is the connection operator

```System.out.println(1+99+"Black horse of the year"); // Black horse of a century
System.out.println(1 + 2 + "ithiema" + 3 + 4 )//Output 3itheima34
System.out.println(1 + 2 + "itheima" + (3 + 4 ) ); //Output 3itheima7
```

## 1.2 assignment operator

1) The assignment operator is used to assign the value of an expression to the left. The left must be modifiable and cannot be a constant

Symboleffectexplain
=assignmenta = 10, assign 10 to variable a
+=Assignment after additiona+=b is equivalent to a = a+b
-=Assignment after subtractionditto
*=Assignment after multiplicationditto
/=Assignment after divisionditto
%=Assign value after taking the remainderditto

2) Note:

The extended assignment operator implies Zhang Zhi's type conversion

## 1.3 self increasing and self decreasing operators

Symboleffectexplain
++Self increasingVariable plus 1
Self subtractionVariable minus 1

matters needing attention:

1. ++And – can be placed either after the variable or before the variable
2. When used alone, + + and -- whether placed before or after the variable, the result is the same
3. When participating in the operation, if it is placed behind the variable, take the variable to participate in the operation first, and then take the variable as + + or –
4. When participating in the operation, if it is placed in front of the variable, first use the variable as + + or –, and then use the variable to participate in the operation

Common usage: used alone

```int i = 10 ;
i++; // Use alone
System.out.println("i:"+i) // i : 11

int j = 10;
++j;  // Use alone
System.out.println("j:"+i) // j : 11

int x = 10;
int y = x++; // In the assignment operation, + + is in the back, so the original x is assigned to y, and X itself increases by 1
System.out.println("x:"+ x + ", y:"+ y);  //x : 11; y : 10
​
int m = 10;
int n = ++m; // Assignment operation, + + is in the back, so the value of M self increment is assigned to n,m, and the value of M self increment is 1
System.out.println("m:" + m + ", n:"+ n); // m:11,n:11
```

practice:

```int x = 10;
int y = x++ + x++ + x++;
System.out.println(y); // y = 10 +11 +12 ; y : 33
```

## 1.4 relational operators (application)

There are 6 kinds of relational operators, namely: less than, less than or equal to, greater than, greater than or equal to, not equal to or equal to

Symbolexplain
==a==b, judge whether the values of a and b are equal, if true, if false
!=a!=b. Judge whether the values of a and B are not equal, true or false
>a> b, judge whether a is greater than b, if true, if false
>=a> = b, judge whether a is greater than or equal to b, if true, if false
<A < b, judge whether a is larger than b, true if true, false if not
<=A < = b, judge whether a is equal to or larger than b, true, false

matters needing attention:

1. The results of relational operators are boolean, either true or false
2. Don't write "=" by mistake. "" is to judge whether it is equal. "=" is to assign value
```int a = 10;
int b = 20;
​
System.out.println(a == b);      //false
System.out.println(a != b);     //true
System.out.println(a > b);    //false
System.out.println(a >= b);     //false
System.out.println( a < b);     //true
System.out.println(a <= b);     //true
​
//The result of a relational operator must be of boolean type, so you can also assign the operation result to a variable of boolean type
boolean flag = a > b;
System.out.println(flag);
​
```

## 1.5 logical operators

Logical operators connect the relational expressions of various operations to form a complex logical expression to judge whether the expression in the program is true or false

Symboleffectexplain
&Logical andA & b, a and b are true, the result is true, otherwise it is false
Logical and
^Logical exclusive orA ^ b, different results of a and b are true, and the same result is false
!Logical non! a. The result is opposite to that of A
```//Defining variables
int i = 10;
int j = 20;
int k = 30;
​
//&Relationship between logic and: as long as one of the expressions is false, the result is false
System.out.println((i > j) & (i > k));  //False & false, output false
System.out.println((i < j) & (i > k));  //True & false, output false
System.out.println((i > j) & (i < k));  //False & true, output false
System.out.println((i < j) & (i < k));  //True & true, output true
​
//|Logical or relationship: as long as one value in the expression is true, the result is true
System.out.println((i > j) & (i > k));  //False & false, output false
System.out.println((i < j) & (i > k));  //True & false, output true
System.out.println((i > j) & (i < k));  //False & true, output true
System.out.println((i < j) & (i < k));  //True & true, output true
​
// ^Logical exclusive or relationship: the same is false, the different is true
System.out.println((i > j) & (i > k));  //False & false, output false
System.out.println((i < j) & (i > k));  //True & false, output true
System.out.println((i > j) & (i < k));  //False & true, output true
System.out.println((i < j) & (i < k));  //True & true, output false
​
// !  Relationship of logical negation: Negation
System.out.println((i>j));  // false
System.out.println(!(i>j));  // false output: true
​
```

## 1.6 short circuit operator

Symboleffectexplain
&&Short circuit andThe function is the same as that of & but has short circuit effect

explain:

1. In the logical and operator, as long as the value of one expression is false, the nam result can be determined as false, and it is not necessary to calculate the values of all expressions
2. Short circuit and operator: once the value is found to be true, the expression on the right will not participate in the operation

Talk about the difference between &, |, & &, | |:

1. &: whether the left side is true or false, the right side must be executed
2. &&: if the left is true, the right is executed; If the left side is false, the right side is not executed
3. |: whether the left side is true or false, the right side must be executed
4. ||: if the left is false, execute on the right; If the left side is true, the right side is not executed
```​
//Defining variables
int x = 3;
int y = 4;
System.out.println((x++ > 4) & (y++ > 5));  //Both expressions operate
System.out.println(x); // x : 4
System.out.println(y); // y : 5
​
System.out.println((x++ > 4) & (y++ > 5));  //The left side can be determined to be false, and the right side does not participate in the operation
System.out.println(x); // x : 4
System.out.println(y); // y : 5
```

## 1.7 ternary operator

1) Ternary operator syntax format:

``` Relational expression ? Expression 1 : Expression 2;
```

2) Explanation:

1. The position in front of the question mark is the judgment condition. The judgment result is boolean. If it is true, expression 1 will be called. If it is false, expression 2 will be called
2. The logic is: if the conditional expression holds or satisfies the execution expression 1, otherwise, the second

3) Examples:

```int a = 10;
int b = 20;

// Judge whether a > b is true. If it is true, take a; if it is false, take B
int c = a > b ? a : b;    // A > b is false, so B is taken
```

# 2. Data input

We can obtain the user's input through the Scanner class. The steps are as follows:

1. Guide bag. The Scanner class is under the java.util package, so it needs to be imported. The package import statement needs to be defined on the class

```import java.util.Scanner
```

2. Create Scanner object

```Scanner input = new Scanner(System.in);
```

```int i = input.Scanner();
```

4. Examples

```import java util.Scanner
public class ScannerDemo {
puiblic static void main(String[] args) {
//Create keyboard input class
Scanner input = new Scanner(System.in);
int x = input.nextInt();
//output data
System.out.println("x:" + x);
}
}
```

# 3. Process control statement

In the process of a program control, the execution order of each statement has a direct impact on the result of the program. Therefore, we must be clear about the execution flow of each statement. Moreover, we often need to control the execution order of statements to achieve the desired functions

## 3.1 classification of process control statements

1. Sequential structure
2. Branch structure (if, switch)
3. Loop structure (for, while, do... while)

## 3.2 sequential structure

1. Sequential structure is the simplest and most basic flow control in a program. There is no specific syntax structure. It is executed in sequence according to the sequence of codes. Most codes in the program are executed in this way

## 3.3 if statement of branch structure

1) if statement format

```Format:
if ( Relational expression ) {
Statement body;
}
```

2) Execution process:

1. First, evaluate the value of the relational expression
2. If the value of the relational expression is true, the statement body is executed
3. If the value of the relational expression is false, the statement body is not executed
4. Continue to execute the following statements

3) Example:

```public static void main(String[] args) {
//Define two variables
int a = 10;
int b = 20;
//Demand: judge whether the values of a and b are equal
if (a == b) {
System.out.println("a be equal to b");
}

int c = 10;
if (a == c) {
System.out.println("a be equal to c");
}

System.out.println("end");
}
```

2) if statement 2

```Format:
if(Relational expression) {
Statement body 1;
}else {
Statement body 2;
}
```

Execution process:

1. First, evaluate the value of the relational expression
2. If the value of the relational expression is true, the statement body 1 is executed
3. If the value of the relational expression is false, the statement body 2 is executed
4. Continue to execute the following statements

example:

``` public static void main(String[] args) {
System.out.println("start");
//Defining variables
int a = 10;
int b = 20;
b = 5;

// Judge
if (a > b) {
System.out.println("a The value of is greater than b");
}else {
System.out.println("a The value of is less than b");
}

System.out.println("end");
}
```

if statement format 3

```Format:
if(Relationship expression 1) {
Statement body 1;
}else if (Relational expression 2) {
Statement body 2;
}else {
Statement body 3;
}
```

Execution process:

1. First, the value of relational expression 1 is evaluated
2. If the value is true, execute statement body 1; If the value is false, the value of relational expression 2 is evaluated
3. If the value is true, execute statement body 2; If the value is false, the value of relational expression 3 is evaluated
4. ...
5. If no relational expression is true, the statement body n+1 is executed

example:

```public static void main(String[] args) {
System.out.println("start");
// Keyboard input class
Scanner input = new Scanner(System.in);
int week = input.nextInt();
if (week == 1) {
System.out.println("Monday");
} else if (week == 2) {
System.out.println("Tuesday");
} else if (week == 3) {
System.out.println("Wednesday");
} else if (week == 4) {
System.out.println("Thursday");
} else if (week == 5) {
System.out.println("Friday");
} else if (week == 6) {
System.out.println("Saturday");
} else {
System.out.println("Sunday");
}
System.out.println("end");
}
```

🎈 After reading it, you can give me some praise, 👉 Your support is my motivation to update every day

Posted by fipp on Mon, 29 Aug 2022 06:14:56 +0930