Java operators include Arithmetic Operators, Relational operators, logical operators, and bitwise operators.
1. Arithmetic Operators
Java Arithmetic Operators include unary operators and binary operators. The operands of arithmetic operators must be numerical values.
(1) unary Operators
The unary operator has only one operand. The unary operator includes: positive (+), negative (-), plus 1 (++), and minus 1.
Variable auto-increment (++), after the variable
int a = 3;int b = a++;System.out.println(a); //4System.out.println(b); //3
The above process is divided into two steps:
int a = 3int b = a; //3a = a + 1; //4
Variable auto-increment (++), before the variable
int a = 3;int b = ++a;System.out.println(a); //4System.out.println(b); //4
The above process is divided into two steps:
int a = 3;a = a + 1; //a = 4int b = a; // b = 4
Auto-subtraction (--) is the same as auto-addition (++). In fact, you only need to add 1 to the variable before and after the operator is in the position of the variable, and then use the variable; if it is behind the variable, use the variable first, and then add 1 to the variable.
(2) binary Operators
Binary operators have two operands. Binary operators include addition (+), subtraction (-), multiplication (*), Division (/), and modulo (% ).
Note: When there are several variables for calculation, the result type depends on the type of the variable with the largest range in these variables.
int a = 3;int b = 2;double c = a / b; // c=1.0
Modulo (%) Example
int a = 4;int b = 3;int c = a % b;System.out.println(c); //c = 1
In the preceding example, A is a positive number, B is a positive number, and C is a positive number.
int a = -4;int b = 3;int c = a % b;System.out.println(c); // c = -1
In the preceding example, A is a negative number, B is a positive number, and C is a negative number.
int a = 4;int b = -3;int c = a % b;System.out.println(c); // c = 1
In the preceding example, A is a positive number, B is a positive number, and C is a positive number.
A rule can be obtained: The result symbol of the modulo operation is the same as the divisor.
2. Relational operators
Java Relational operators are used to compare the size of two values. The result of calculation is a Boolean value. There are 6 Relational operators: greater than (>), less than (<), equal to (=), not equal (! =), Greater than or equal to (> =), less than or equal to (<= ). I will not go into details here.
3. logical operators Java logical operators require that the data type of the operands be boolean, and the calculation result is also Boolean. Logical operators include Logic and (&), logic or (| and |), non-logical operators (!), Logical XOR (^ ). Among them, the logic is basically the same as & and &, the logic is or | and |, and the calculation rules are basically the same. The difference is: & and | the operation completes the calculation of all logical expressions; & | short circuit function is provided for operations. Short-circuit feature of logical operators (1) logic and: if the first operand is false, the result must be false, and the operation following the operator will not be executed.
int a = 10;int b = 11;int c = 12;int d = 13; boolean e = (a > b) && ((b = c) < d);System.out.println(e); // e = falseSystem.out.println(b); // b = 11
However, if the first one is true, the operation following the operator will be executed normally. For example (this is only to verify the short-circuit feature)
int a = 10;int b = 11;int c = 12;int d = 13; boolean e = (a < b) && ((b = c) > d);System.out.println(e); // e = falseSystem.out.println(b); // b = 12
(2) logic or: if the first operand is true, the result must be true, and the operation following the operator will not be executed. The example is similar to the logic. I will not repeat it here.
4. bitwise operators
The bitwise operators of Java are computed in binary units. Their operands and calculation results are both integer. There are 7 bitwise operators: bitwise AND (&), bitwise OR (|), bitwise NOT (~), Bitwise exclusive or (^), right shift (>>), left shift (<), right shift of 0 fill (>>> ).
Bitwise operations: bitwise AND (&), bitwise OR (|), bitwise non (~) The bitwise OR (^) is exactly the same as the true value table of the corresponding logical operation. The difference is that the operands and results of bitwise operation are both binary integers, the operations and results of logical operations are logical values. This is not an example. It is no problem for students with a computer base. There is a classic exam:
/** Methods used in Java to achieve the highest efficiency of 2x8 */system. Out. println (2 <3); // 16
Next, review the Java basics-process control statements.