Common Operators
Value assignment operator: =
Compound assignment operator: + =,-=, * =,/=, % =
Arithmetic Operators: +,-, *,/, % (distinct binary operator), ++, -- (distinct unary operator)
Conditional operators:>, <, >=, <= ,! =, =
Logical operators: &, |, | ,!
Almost all operators can only operate on basic data types, but "=", "=" and "! = ", These operators can operate on all objects. In addition, the String class supports "+" and "+ = ".
Priority
When an expression contains multiple operators, the operator priority determines the order of operation of each part of the expression. Operators in Java have their own set of computing sequence. Remember the sequence to avoid Operation errors in the program. The simplest thing is to multiply, divide, and add or subtract
Operator priority
Postfix operators |
[]. (Params)Expr++Expr-- |
Unary operators |
++Expr--Expr+Expr-Expr~ ! |
Creation or cast |
New (Type)Expr |
Multiplicative |
*/% |
Additive |
+- |
Shift |
<>>>> |
Relational |
<> <=> = Instanceof |
Equality |
=! = |
Bitwise AND |
& |
Bitwise exclusive OR |
^ |
Bitwise aggressive OR |
| |
Logical AND |
&& |
Logical OR |
| |
Conditional |
? : |
Assignment |
= + =-= * =/= % = <<=>>>>> = |
Note: the priority of the above operations is reduced from top to bottom, and the priority of the same cell is the same
When programming, we will inevitably forget the sequence of operators, so we should use parentheses to clearly define the order of operation.Copy codeThe Code is as follows: public class Precedence {
Public static void main (String [] args ){
Int x = 1, y = 2, z = 3;
Int a = x + y-2/2 + z;
Int B = x + (y-2)/(2 + z );
System. out. println ("a =" + a + "B =" + B );
}
}
Output
A = 5 B = 1
The "+" in the output statement means "string connection" in this context. If necessary, it also needs to perform "String Conversion ". When the compiler observes that a String is followed by a "+", and the "+" is followed by a non-String element, it will try to convert this non-String type element to the String type.
Assignment
The value assignment operator "=", which means to take the value on the right (right value) and copy it to the left (left value ). The right value can be any constant, variable or expression, or any method that can generate a value. But the left side must be a clearly named variable. That is to say, there must be a room space that can store the value on the right of the equal sign. For example, you can assign a constant to a variable.
A = 4
However, you cannot assign anything to a constant. a constant cannot be left 4 =.
Basic data stores actual values instead of references to an object. Therefore, when assigning a value to an object, the content in one place is copied to another place. For example, if the basic data type is a = B, the actual meaning is to copy the content in B to a. If a is assigned a value later, B will not be affected. However, not all assignments will achieve this expected effect.
When assigning values to an object, we actually operate on the object reference. Therefore, if we assign an object to another object, we actually copy the "Reference" from one place to another, this means that the value of one of the objects will change with the other.Copy codeThe Code is as follows: class Tank {
Int level;
}
Public class Assignment {
Public static void main (String [] args ){
Tank t1 = new Tank ();
Tank t2 = new Tank ();
T1.level = 9;
T2.level = 47;
System. out. print ("1: t1.level" + t1.level + ", t2.level" + t2.level );
T1 = t2;
System. out. print ("2: t1.level" + t1.level + ", t2.level" + t2.level );
T1.level = 27;
System. out. print ("3: t1.level" + t1.level + ", t2.level" + t2.level );
}
}
Output
1: t1.level: 9, t2.level: 47;
2: t1.level: 47, t2.level: 47;
3: t1.level: 27, t2.level: 27;
In this example, the Operation Reference problem occurs. We modified T2. in most cases, t1 and t2, however, because the assignment operation is an object reference, t1 and t2 here contain the same references, which point to the same object (originally t1 contains references to objects, is an object pointing to a value of 9. When t1 is assigned a value, this reference is overwritten, that is, lost. The object that is no longer referenced will be automatically cleared by the garbage collector.
This special phenomenon is often called the alias phenomenon, which is a basic method for Java to operate objects. In this example, if you want to avoid aliases, you should perform operations on the object values directly:
T1.level = t2.level;
In this way, writing can keep two objects independent, but directly operating on the object's domain can easily lead to confusion, and violates the good object-oriented programming principles.
The following example describes the alias in a method call.Copy codeThe Code is as follows: class Letter {
Char c;
}
Public class PassObject {
Static void f (Letter y ){
Y. c = 'Z ';
}
Public static void main (String [] args ){
Letter x = new Letter ();
X. c = 'a ';
System. out. print ("1: x. c" + x. c );
F (x );
System. out. print ("2: x. c" + x. c );
}
}
Output
1: x. c:
2: x. c: z
When we use the f method, it seems that it copies a copy of its parameter Letter y in its scope; but it actually only passes a reference. So the code line
Y. c = 'Z ';
Actually, objects other than f () are changed.
Other value assignment operators
Operator |
Use |
Equivalent |
+ = |
Op1 + = op2 |
Op1 = op1 + op2 |
-= |
Op1-= op2 |
Op1 = op1-op2 |
* = |
Op1 * = op2 |
Op1 = op1 * op2 |
/= |
Op1/= op2 |
Op1 = op1/op2 |
% = |
Op1 % = op2 |
Op1 = op1 % op2 |
& = |
Op1 & = op2 |
Op1 = op1 & op2 |
| = |
Op1 | = op2 |
Op1 = op1 | op2 |
^ = |
Op1 ^ = op2 |
Op1 = op1 ^ op2 |
<= |
Op1 <= op2 |
Op1 = op1 <op2 |
>>= |
Op1> = op2 |
Op1 = op1> op2 |
>>>= |
Op1> = op2 |
Op1 = op1> op2 |