4 Java Foundation__ operator

Posted by stemp on Wed, 02 Mar 2022 10:05:18 +0100

Operator classification:

Arithmetic operator:

① Except (/):

class AriTest 
{
	public static void main(String[] args) 
	{
		int num1=12;
		int num2=5;
		int result1=num1/num2;
		double result2=num1/num2;
		double result3=num1/num2+0.0;
		double result4=num1/(num2+0.0);
		double result5=(double)num1/num2;
		System.out.println(result1);  //2
		System.out.println(result2);  //2.0; Because the result of num1/num2 is int, it is equal to 2, and 2 is assigned to double Become 2.0
		System.out.println(result3);  //2.0;
		System.out.println(result4);  //2.4;
		System.out.println(result5);  //2.4;

	}
}

② Remainder (%): (the symbol of the result of the remainder operation is the same as that of the module, and% is often used to judge whether it can be divided or not in development)

class GetRest 
{
	public static void main(String[] args) 
	{
		int m1=12;
		int n1=5;
		System.out.println("m1%n1="+m1%n1);  //2

		int m2=-12;
		int n2=5;
		System.out.println("m2%n2="+m2%n2);  //-2

		int m3=12;
		int n3=-5;
		System.out.println("m3%n3="+m3%n3);  //2

		int m4=-12;
		int n4=-5;
		System.out.println("m4%n4="+m4%n4);  //-2
		
	}
}

③ Self increasing and self decreasing operation (+ +, –):

class AddAdd 
{
	public static void main(String[] args) 
	{
		int a1 =10;
		int b1=a1++;
		System.out.println("a1="+a1+","+"b1="+b1);  //a1=11,b1=10, calculate first and then add one
		int a2=10;
		int b2=++a2;
		System.out.println("a2="+a2+","+"b2="+b2);  //a2=11,b2=11, add one before operation
		int a3=10;
		a3++; //Change to + + a3 and the result is 11. Pay attention to the difference. Here is the independent operation first
		int b3=a3;
		System.out.println("a3="+b3);  
		//Note ①:
		short s1=10;
		//s1=s1+1; Compilation will fail here, which does not comply with the automatic type conversion rules
		s1=(short)(s1+1);  //correct; Or s1 + + (self increment operation will not change the data type of the variable itself, which is more efficient)
		//Note ②:
		byte bb1=127;
		bb1++; 
		System.out.println("bb1="+bb1); //The compilation can pass, and the result is - 128. It can be explained through binary knowledge. See the figure below for details
	}
}

Operation results:
Explanation:


Exercise 2:

/*
Arbitrarily give a three digit integer, print and display its single digit, ten digit and hundred digit values.
The format is as follows:
The number xxx is as follows:
Single digit:
Ten digits:
Hundredths:
For example:
The number 153 is as follows:
Single digit: 3
 Ten digits: 5
 Hundreds: 1
*/
class AriExer 
{
	public static void main(String[] args) 
	{
		int num=187;
		int bai=num/100;
		int shi=num%100/10;
		int ge=num%10;
		System.out.println("Hundred is"+bai);

		System.out.println("Ten are"+shi);

		System.out.println("One bit is"+ge);
	}
}

Assignment Operators

Continuous assignment, such as i1=j1=10, can also be written as int i1=10,int j1=10; (i1 and j1 are considered as integers)

class SetValueTest 
{
	public static void main(String[] args) 
	{
		int num1=10;
		num1+=2;  //Equivalent to num=num+2;
		System.out.println(num1);
		int num2=12;
		num2%=5;  //Equivalent to num2=num2%5;
		System.out.println(num2);
		System.out.println("**********************");
		//Subtle differences between the two methods:
		short s1=10;
		//s1=s1+2;  // Compilation failed
		s1+=2;  //After compilation, the data type of the variable itself will not be changed
		System.out.println(s1);
		System.out.println("**********************");
		//In development, if you want to realize + 2 operation, how many methods are there? (premise: int num=10;)
		//Mode 1: num=num+2;
		//Mode 2: num+=2; (it is recommended that the amount of code is small and the variable data type is not changed.)
		//Add one to the implementation variable: ① num=num+1; ②num+=1; ③ Num + + (recommended)
	}
}

Exercise:

class Exce
{
	public static void main(String[] args) 
	{
		int i = 1;
		i *= 0.1;  //Note that the data type of the variable itself is not changed here, so it is compiled and truncated to 0.1, and the result is 0
		System.out.println(i);  //0
		i++;
		System.out.println(i); //1
	}
}

class Exce
{
	public static void main(String[] args) 
	{
		int m = 2;
		int n = 3;
		n *= m++;  //n=n*m++;
		System.out.println("m=" + m); //Output 3
		System.out.println("n=" + n); //Output 6
	}
}

class Exce
{
	public static void main(String[] args) 
	{
		int n = 10;
		n += (n++) + (++n);  //n=n+(n++)+(++n)
		System.out.println(n);  //32;10+10+12=32

	}
}

Comparison operator

Logical operator

Logical XOR (true when different) is true when the two sides are different, and false when the two sides are the same.

About the difference between logic and & and short circuit and & (see the following two codes, which can deduce logic or | and short circuit or |):

Case 1:

/*
Logical operators operate on Boolean variables

*/
//Use test of logical operators

class LogicTest 
{
	public static void main(String[] args) 
	{
		//Distinguish between & (logic and) and & & (short circuit and)
		boolean b1=true;
		int num1=10;
		if(b1&(num1++>0)){
			System.out.println("I'm in Beijing now");
		}else{
			System.out.println("I'm in Nanjing now");
		}
		System.out.println("num1="+num1);
		System.out.println("*************");
		boolean b2=true;
		int num2=10;
		if(b2&&(num2++>0)){
			System.out.println("I'm in Beijing now");
		}else{
			System.out.println("I'm in Nanjing now");
		}
		System.out.println("num2="+num2);

	}
}

Here b1 and b2 are true
The results of logic and short circuit are the same.

Scenario 2:

/*
Logical operators operate on Boolean variables

*/
//Use test of logical operators

class LogicTest 
{
	public static void main(String[] args) 
	{
		//Distinguish between & (logic and) and & & (short circuit and)
		boolean b1=true;
		b1=false;
		int num1=10;
		if(b1&(num1++>0)){
			System.out.println("I'm in Beijing now");
		}else{
			System.out.println("I'm in Nanjing now");
		}
		System.out.println("num1="+num1);
		System.out.println("*************");
		boolean b2=true;
		b2=false;
		int num2=10;
		if(b2&&(num2++>0)){
			System.out.println("I'm in Beijing now");
		}else{
			System.out.println("I'm in Nanjing now");
		}
		System.out.println("num2="+num2);

	}
}

Here b1 and b2 are false
The operation found that the logic and short circuit were different from the result
In case 2, the logic and part of the code performed the self increment operation, and the short circuit and did not perform the self increment operation
When the left side of the symbol is true, both will perform the operation on the right side of the symbol; When the left side of the symbol is false, & continue the operation on the right side of the symbol&& The operation to the right of the symbol is no longer performed.
b2 has been set to false. Whether the right side is true or false will not affect the result of short circuit and operation

Similarly: distinguish between | and | when: when the left side of the symbol is false, both will perform the operation on the right side of the symbol. When the left side of the symbol is true, | continue to perform the operation on the right side of the symbol, | no longer perform the operation on the right side of the symbol.

Short circuit and short circuit or are recommended in development.

Logical operator exercise:
Analysis: x++=2 is false, X is 2 after execution, continue execution, right + + y==2 is true, y is 2 after execution; The final output X and y are both 2.

The final output x is 2 and y is 1.

The final output x is 7 and y is 2.


The final output x is 7 and y is 1.

Bitwise operator (not commonly used in development)

Note: both logical operators and bitwise operators have &, |, ^; Whether it is a bit operator or a logical operator depends on the operand. Two numbers are bit operations; Two Booleans are logical operations.
Note that 21 > > 2 (shift to the right) in the above figure. If the highest bit is 0 (indicating a positive number), use 0 to supplement the left; If the highest bit is 1, fill the left with 1
Summary:
① Bitwise operators operate on integer data
② < <: within a certain range, each shift to the left is equivalent to multiplying by 2. >: Within a certain range, each shift to the right is equivalent to dividing by two. (negative numbers are the same)

Classic interview questions:
The most efficient way to calculate 2 * 8?
Answer: 2 < < 3 or 8 < < 1 (the order of magnitude is constant)
Reverse: (less used)

Bitwise operator exercise:
Exchange the values of two variables
int num1=10;
int num2=20;

Define temporary variables

class ExTwo
{
	public static void main(String[] args) 
	{
		int num1=10;
		int num2=20;
		int temp=num1;
		num1=num2;
		num2=temp;
		System.out.println("num1="+num1+",num2="+num2);
	}
}

Ternary operator


practice:
①:

//Gets the maximum value of two integers
class TriYuan 
{
	public static void main(String[] args) 
	{
        int m=12;
		int n=5;
		int max =(m>n)?m:n;  
		System.out.println(max);
//m. N the specific data types are not required to be the same, but there should be a unified type to receive. eg:double num=(m>n)? 2:1.0; It's OK.
		System.out.println("****************");
		n=12;
		//Ternary operators can be nested
		String maxStr=(m>n)?"m large":((m==n)?"m and n equal":"n large");
		System.out.println(maxStr);
	}
}

②:

//Gets the maximum value of three integers
class TriYuan 
{
	public static void main(String[] args) 
	{
        int n1=12;
		int n2=30;
		int n3=-43;
		int max1 = (n1>n2)?n1:n2;
		int max2 = (max1>n3)?max1:n3;
		System.out.println("The maximum of the three numbers is:"+max2);
	}
}

Tips:
Where ternary operators can be used, they can be rewritten as if else statements; On the contrary, it is not established; If both are available, ternary operators are preferred because they are concise and efficient.

Topics: Java Back-end