1. Operator
Operators include the following:
Arithmetic operator assignment operator comparison operator logic operator bit operator trigonometric operator
The least commonly used bit operators are also the ones closest to the underlying computer.
1. Arithmetic operators
(1) Several usages of +: addition, positive numbers, string concatenation characters
(2) One problem should be paid attention to when dividing integers: divide them together and you can only get integers. To get a decimal, you can convert the data itself *1.0, that is, convert the data itself into a floating point type first.
2. Assignment operator
Symbol = += -= *= /= %=
Note: = is the basic assignment operator, and the others are extended assignment operators
Interview questions:
(1) short s=1, s = s+1;
(2) short s=1, s+=1;
Is there any problem with the above two codes? If so, where is there any problem?
Answer: Code (1) is incorrect and will lose accuracy. When defining byte and short, they actually receive a value of type int. This is done by myself. If it is no longer within their range, an error will be reported. The effects are as follows:
Code (2) has no errors because: the extended assignment operator actually implies a cast.
That is, i += 1; is not equivalent to i = i + 1; but is equivalent to i = (data type of i)(s + 1);
3. Relational operators
Note 1: The results of the comparison operator are boolean, that is, they are either true or false.
Note 2: The comparison operator "==" cannot be mistakenly written as "=". For example:
4. Logical operators
(1) Logical operators are used to connect Boolean expressions. They cannot be written as 3<x<6 in Java, but should be written as x>3 & x<6.
(2) The difference between "&" and "&&"? Similarly, is the difference between "|" and "||"?
A: The final result is the same. For example: A and B must be true at the same time, and the results of A&B and A&B are true. B:&& has a short circuit effect, the left side is false, and the right side is not executed. ||It has a short circuit effect, the left side is true, and the right side is not executed.
Note: Commonly used logical operators in development: &&,||,!
(3) The difference between XOR ( ^ ) and OR ( | ) is: when both left and right are true, the result of XOR is false.
5. Bit operator:
Although not much is used in development, it will be seen in many source codes because the underlying computing of computers is bit operations.
Interview Question 1: Implement the exchange of two integer variables
The code is as follows:
/* Interview question: Please implement the exchange of two integer variables by yourself*/class OperatorTest { public static void main(String[] args) { int a = 10; int b = 20; System.out.println("a:"+a+",b:"+b); //Method 1: Use third-party variables (used in development) int c = a; a = b; b = c; System.out.println("a:"+a+",b:"+b); System.out.println("----------------"); //Method 2: Use bit XOR to implement (interview) //Left: a,b,a //Right: a ^ ba = a ^ b; b = a ^ b; //a ^ b ^ b = aa = a ^ b; //a ^ b ^ a = b System.out.println("a:"+a+",b:"+b); //Method 3: Use variables to add a = a + b; //a=30 b = a - b; //b=10 a = a - b; //a=20 System.out.println("a:"+a+",b:"+b); * //Method 4: Get it done in one sentence b = (a+b) - (a=b); //b=30-20=10,a=20 System.out.println("a:"+a+",b:"+b); }}Interview question 2: Please write the result of calculating 2 times 8 in the most efficient way.
Answer: 2 * 8 is equivalent to 2 << 3
Knowledge Review:
<<:Discard the highest bit on the left and the right side. >>:The highest bit on the right is 0, and the left side is 0; the highest is 1, and the left side is 1 >>>:Unsigned right shift no matter whether the highest bit is 0 or 1, the left side is 0, and the left side is 0.
6. Three-point operator:
Format: (relational expression)? Expression 1: Expression 2;
If the condition is true, the result after the operation is expression 1;
If the condition is false, the result after the operation is expression 2;
Example:
Get the large number of two numbers:
The copy code code is as follows: int x=3,y=4,z;z = (x>y)?x:y;//z variable stores a large number of two numbers.
Get the maximum value of three integers:
//Method 1: In two steps //A: Compare the maximum values of a and b first //B: Compare the maximum values of a and b with c int temp = ((a > b)? a: b); //System.out.println(temp); int max1 = (temp > c? temp: c); System.out.println("max1:"+max1); //Method 2: Get int max2 in one step = (a > b)?((a > c)? a: c):((b > c)? b: c); //This method is not recommended: //int max2 = a > b?a > c? a: c:b > c? b: c; //System.out.println("max2:"+max2);It is recommended to use method one. In addition, in Mode 2, line 10 and line 12 are the same, and are nested use of the trigonometric operator.
Compare whether two integers are equal:
//Compare whether two integers are equal int m = 100; int n = 200; //boolean flag = (m == n)? true: false; boolean flag = (m == n); System.out.println(flag);
2. Keyboard data entry
When we are writing programs, the data values are fixed, but in actual development, the data values must change, so I am planning to improve the data into keyboard input to improve the flexibility of the program.
How to implement keyboard data entry? (Remember to use it now)
(1) Transmission package (position on top of class definition): import java.util.Scanner;
(2) Create keyboard entry object: Scanner sc = new Scanner(System.in);
(3) Get data through the object: int x = sc.nextInt();
Example code:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.