1. Operators(operator)
The Java language supports the following operators:
arithmetic operators : +,-,*,/,%, ++
assignment Operator =
relational operators : >,<,>=,<=,= = ,!= instanceof
logical operators : &&,| | , !
bitwise operators : &,| , ^ , ~ , >> , << , >>>
Conditional Operators ?
Extended Assignment Operators : + =,-=,*=,/=
2.Unary Operators(++, --)
int a = 3; int b = a++; // after execution, b=3. Assign a value to B first and then increment it. int c = ++a; // after execution, c=5. Increment first, then assign a value to B
Note: the exponentiation in Java is handled:
int a = 3^2; // this cannot be handled in Java, ^ is an XOR symbol. double b = Math.pow (3, 2);
3. Two-dollar operator
Integer operations:
if the two operands have one Long, The result is also a long
When there is no long , the result is int. Even if the operands are all shot,byte, the result is an int.
Floating-point arithmetic:
If two operands have one double, The result is double.
only two of the operands are float, The result is float.
4.Boolean logical expression
logic with:&& and &, logic or:| | and the | , logical non-:!.
Logic and logic or the use of short-circuit methods . From left to right, if the determined value is no longer evaluated.
The logic returns false directly if there is a false.
logical OR as long as there is a true, then directly returns true;
Boolean c = 1>2&&2> (3/0);
5. Bitwise operators
~-- Take counter &-- bitwise AND
| --Bitwise or ^--bitwise XOR
<<: Left shift operator, >>: Right shift operator >>>: Unsigned shift operator
move right one is equivalent to 2 pick-up.
shift left one is equivalent to multiply by 2.
int a = 3*2*2; int b = 3<<2; // equivalent: 3*2*2; int a = 12/2/2; int B = 12>>2;
6. Extension operators
Operator |
Examples of usage |
An equivalent expression |
+= |
A + = b |
A = A+b |
-= |
A-= b |
A = a-b |
*= |
A *= b |
A = A*b |
/= |
A/= b |
A = A/b |
%= |
A%= b |
A = A%b |
7. String connector
as long as one of the operands on either side of the "+" operator is a string (string) type , the system automatically converts the other operand to a string and then connects.
8. Three Mesh condition operator
Three-mesh conditional operator, syntax format:
X? Y:z
where x is a boolean expression, the value of X is evaluated first , and if true, the result of theentire trinocular operation is the value of the expression y . Otherwise the entire operation result is the value of the expression Z.
Problem with operator Precedence:
R to L
|
. ( ) { } ; , ++ -- ~ ! (data type) |
L to R |
* / % |
L to R |
+ - |
L to R |
<< >> >>> |
L to R |
< > <= >= instanceof |
L to R |
== != |
L to R |
& |
L to R |
^ |
L to R |
| |
L to R |
&& |
L to R |
|| |
R to L |
? : |
R to L |
= *= /= %= + = = <<= >>= >>>= &= ^= |= |
CODE schematic:
Public classtestoperator{ Public Static voidMain (string[] agrs) {//Simple Operators intA = 1; intb = 2; intc = A +b; System.out.println ("The value of C is:" +c); //Unary Operators intD = 1; inte =d++;//after execution, d=1. Assign the E value first and then increment intf =++d;//after execution, f=3. Increment first, then assign value, here Increment 2 times, in E that already self-increment once. System.out.println ("E's value is:" +e); System.out.println (The value of "F" is: "+f); //Bitwise Operators intm = 2; intn = 4; System.out.println (M&N); System.out.println (M|N); System.out.println (~N); System.out.println (M^N); //Three mesh operator intA1 = 10; intA2 = 20; System.out.println ((A1>A2)?false:true); }}
The "Java" Java_11 operator