Han Shunping Java03 (operator)

 

operator

1. Arithmetic operator arithmetic

  • Use of division / Division

Look at a piece of code:


System.out.println(10 / 4); //The output is 2. Because two integers are divided, the result is received as an integer by default

System.out.println(10.0 / 4);//The output is 2.5

double d = 10 / 4;
System.out.println("d=" + d);//d=2.0

 

This is equivalent to dividing two integers, so the result is received as an integer by default, and then the number of int type 2 is assigned to double type, so it is 2.0

 

  • Modular operation (remainder) - the positive and negative results are consistent with the modulus to be taken

System.out.println(10 % 3);//1

System.out.println(10 % -3);//1 merchant-3 Yu 1

System.out.println(-10 % 3);//-1 Shang 3 Yu-1

System.out.println(-10 % -3);//-1 merchant-3 more than-1

 

  • Auto increment / Auto decrement operation:

int j = 8;

int k = ++j;//Equivalent to j=j+1; k=j

int k = j++ //Equivalent to k=j; j=j+1

 

Others, and so on

++As an independent statement, + + i and i + + are absolutely equivalent, but as an assignment statement, the above rules are followed

A classic interview question:


int i = 1;
i =  i++;
System.out.println(i);//1
//Here, a temporary variable will be used because it is assigned to itself
//(1)temp = i
   (2)i = i + 1
   (3)i = temp

int i = 1;
i =  ++i;
System.out.println(i);//2
  (1)i = i + 1
   (2)temp = i
   (3)i = temp  

 

example:


int i1 = 10;
int i2 = 20;
int i = i1++;
System.out.println("i="+i);//i=10
System.out.println("i2="+i2);//i2=20
i = --i2;//This is assignment, but i2 Self subtraction has been performed, so i2 It became 19
System.out.println("i="+i);//i=19
System.out.println("i2="+i2);//i2=19

 

When self incrementing is assigned to itself, such as i=i + +, the result of i will not change, but when assigned to other variables, the self incrementing variables (during assignment) will change. int j = i + +, and i has + 1


int x = 5;
System.out.println(x++==6); //false Here we compare first and then increase 
System.out.println(x); //6

 

 

practice


int day = 21456;
System.out.println("also"+ (day/7) + "Week zero" + day % 7 + "Day off");
double degree = 1234.6;
System.out.println("fahrenheit" + degree + "It's Celsius" + (degree-100)*5/9);

 

(degree-100)5/9 can also be written as 5.0/9 (degree-100), but it cannot be written as 5 / 9 * (degree-100). The result will be 0.0 because the result of 5 / 9 is 0

2. Relational operators

  • The results of relational operation are boolean values. Relational operation expressions are often used in the conditions of if structure or loop structure

  • Equality is = =, and a = is an assignment

 

 

3. Logical operators

  • It is used to connect multiple conditions (expressions), and the final result is also a boolean value

  • Short circuit operation (& & and |) is basically used in practical operation

 

 

 

 

 

 

 

  • Short circuit and (& &) and logic and (&)

For short circuit and (& &), if the first condition is judged as false, the second condition will not be judged, which is more efficient. Logic and continue to judge the second condition even if the first condition is judged as false.


int a = 5;
int b = 15;
if(a < 1 && ++b >10){
//Although judgment a<1 False but still executed++b,If a The condition is true++b It will still be implemented
    System.out.println("666");
}
System.out.println("a = " + a + "b = " + b); //a = 5 b = 15
if(a < 1 & ++b >10){
    System.out.println("666");
}
System.out.println("a = " + a + "b = " + b);//a = 5 b = 16

 

  • Short circuit or (|) and logic or (|)

Short circuit or (|): if the first condition is true, the second condition will not be judged.

Logical or (|): if the first condition is true, the second condition will also be judged.

int a = 5;
int b = 15;
if(a < 10 || ++b >10){
    System.out.println("666");
}
System.out.println("a = " + a + "b = " + b); //666 a = 5 b = 15
​
​
if(a < 10 | ++b >10){
    System.out.println("666");
}
System.out.println("a = " + a + "b = " + b);//666 a = 5 b = 16

 

  • Logical non (!) and logical exclusive or (^) inverse: opposite

boolean flag = true;
boolean flag2 = !flag;
System.out.println(flag ^ flag2); //true

 

Logical XOR (^): if two conditions are different, it is true, otherwise it is false

  • practice


boolean x = true;
boolean y = false;
short z = 46;
if ((z++==46) && (y = true)) z++;
if ((x=false) || (++z==49)) z++; //here x=false False
System.out.println("z=" + z); //z=50
System.out.println("x=" + x + " " + "y=" + y); //x=false y=true

 

In the if statement, if x is assigned as true, it is true, and if it is assigned as false, it is false. Pay attention to assignment and judgment

4. Assignment operator

  • Normal assignment:

(1) Operation order from left to right int num = a + b + c

(2) The left side of the assignment operator can only be variables, and the right side can be variables, expressions and constant values.

  • Compound assignment operator:

a+=1 is equivalent to a = a + 1, and so on

Compound assignment operators (and self incrementing operations) cast types

byte b = 127;
b+=1;
System.out.println(b);//128
byte b = 2;
b+=1;
b++;

 

As we said before, byte, short and char will be automatically converted to int type during operation, but here:

b+=1; Equivalent to b = (byte) (b + 1)

b + +; equivalent to b = (byte)(b + 1)

If the above expression is changed to:

byte b = 2;
b=b+1;

 

An error will be reported: there may be a loss when converting from int to byte

5. Ternary operator

 

 


int a = 10, b = 20;
int result = a > b ? a++ : b--;
 System.out.println(result); //20
 System.out.println(a); //10
 System.out.println(b); //19

 

Here, a > b determines that the result is false, so when the second expression B --, B -- is assigned to result, it is assigned first and then - 1, so the output of result is 20. Because the first expression is not executed, the value of a remains unchanged.

  • Note: the types of the two expressions should be consistent with the types of the received variables. If they are inconsistent, automatic type conversion (assigning small units to large units) can also be carried out, or forced type conversion can be added manually


int a = 10, b = 20;
int c = a > b ? 1.2 : 1.5; //x Wrong writing
double c = a > b ? 1.2 : 1.5; //

int c = a > b ? (int)1.2 : (int)1.5; //

 

  • Exercise: find the maximum of three numbers

int a = 10, b = 20, c = 6;
int max1 = a > b ? a : b;
int max2 = max1 > c ? max1 : c;
System.out.println("The maximum value is:" + max2); //20

 

Added by hemantraijain on Sat, 04 Dec 2021 03:17:03 +0200