# Arithmetic operator

## Precautions for arithmetic operators

• 1. The sign of the result is the same as that of the module
• 2. In development, we often use% to judge whether it can be eliminated
• 3. "+" can be a connector. In addition to the string addition function, it can convert non string into string.

For example:

```System.out.println("5+5="+5+5);//Convert non string to string
```

The results are:

```5+5=55
```
• 4. Self increment / self decrement does not change the data type of its own variable
```s1++;
++s1;//When used alone, the two formulas have no difference and will not change the data type of the variable itself

//There are differences when used with other operators. See the arithmetic operator table above
```

# Assignment Operators

• 1. Symbol:=
• When the data types on both sides of "=" are inconsistent, you can use automatic type conversion or forced type conversion principle for processing
• Continuous assignment is supported

give an example:

```int i1,j1;
i1 = j1 = 10;
```
• Extended assignment operators: + =, - =, * =, / =,%=
• Extending the assignment operator does not change the type of the variable itself
give an example:
```int i1 = 10;
i1 +=  10; //Does not change the data type of the variable itself
```

# Comparison operator

• 1. The results of comparison operators are boolean, that is, they are either true or false
• 2. The comparison operator '= =' cannot be mistakenly written as' = '
• 3. Explain
• == ， != : You can use not only data of numeric type, but also data of reference data type, such as String type.
give an example:
```Account acct1 = new Account(1000);
Account acct2 = new Account(1000);
boolean b1 = (acct1 = acct2); //Compare whether two accounts are the same Account
boolean b2 = (acct1 != acct2);
```
• <, >, > =, < =: can only be used between numeric data types
• 4. instanceof subsequent supplement
Typical code:
```//Example 1
int i = 20;
int j = 19;
System.out.println(i == j);//false
System.out.println(i = j);//19

//Example 2
boolean b1 = false;
boolean b2 = true;
System.out.println(b1 == b2);
System.out.println(b1 = b2);

```

# Logical operator

• 1. Logical operators are used to connect Boolean expressions. In Java, they cannot be written as 3 < x < 6, but should be written as x > 3 & x < 6. The result is also Boolean
-2. The difference between "&" and "& &":
• ① In single & case, the operation is performed on the right regardless of whether the left side is true or false
• ② When double &, if the left is true, the right participates in the operation. If the left is false, the right does not participate in the operation

Typical code 1:

```int x = 1;
int y = 1;
if(x++==2 & ++y==2)
{
x =7;
}
System.out.println("x="+x+",y="+y);

int x1 = 1;
int y1 = 1;
if (x1++==2&&++y1==2)
{
x1 = 7;
}
System.out.println("x1="+x1+",y1="+y1);
```
• 3. The difference between "|" and "|":
• ||Indicates that when the left is true, the right does not participate in the operation

Typical code 2:

```int x2 = 1;
int y2 = 1;
if(x2++==1|++y2==1)
{
x2 = 7;
}
System.out.println("x2="+x2+",y2="+y2);

int x3 = 1;
int y3 = 1;
if(x3++==1||++y3==1)
{
x3 = 7;
}
System.out.println("x3="+x3+",y3="+y3);
```
• 4. The difference between XOR (^) and or (|) is that when both left and right are true, the result is false
• Understanding: XOR pursues "difference"! The difference is true and the same is false

## practice

```class Test
{
public static void main(String args[])
{
boolean x = true;
boolean y = false;
short z=42;
if((z++==42)&&(y=true))
z++;
if((x=false) || (++z==45))
z++;
System.out.println("z="+z);
}
}
```

result:

```z = 46
```

# Ternary operator / ternary operator

• 1. Format: (conditional expression)? Expression: 1;
• The conditional expression is true, and the result of the operation is expression 1; Is false, and the result of the operation is expression 2;
• 2. Expression 1 and expression 2 are of the same type. What I understand here is that both are basic data types. Basic data types and reference data types cannot be used at the same time

Correct example:

```int m = 19;
int n =8;
int max = (m > n)? m : n;
```

Examples of errors:

```String c1 = (m > n)? 2 :  "output";
String s1 = (m > n)? 'a' : "output"
```
• 3. Relation and difference between ternary operator and if else:
• ① Ternary operators simplify if else statements
• ② Ternary operators require that a result be returned
• ③ The code block after if can have multiple statements
• 4. Ternary operators can be nested
• 5. Any ternary operator that can be used can be rewritten into an if else statement. On the contrary, it does not hold

For example, reasons for not holding water:

```int m = 19;
int n =8;
if (m>n)
{
System.out.println(m);
}
else
{
System.out.println("character string");
}

// The following is an example of an error if the compilation fails
String ss1 = (m > n)? "character string" : m;
System.out.println(ss1);

```
• 6. If the program can use both ternary operators and if else statements, the ternary operator is preferred.
• Reason: simplicity and high operation efficiency

Keywords: Java jdk8

Added by franknu on Sat, 15 Jan 2022 12:21:32 +0200