Common operators
Assignment operator:=
Compound assignment operators: +=, -=, *=, /=, %=
Arithmetic operators: +,-,*,/,%(ß binary operator),++,--(ß 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 multiple operators exist in an expression, the priority of the operator determines the order of operations of each part of the expression. Operators in Java have their own set of calculation orders. Keeping the order in mind can prevent operational errors from occurring in the program. The easiest thing is to multiply and divide first and then 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 inclusive OR |
| |
logical AND |
&& |
logical OR |
|| |
conditional |
? : |
assignment |
= += -= *= /= %= &= ^= |= <<= >>= >>>= |
Note: The operands above are reduced from top to bottom, and the priority in the same cell is the same.
When we are programming, we will inevitably forget the order of operators, so we should use brackets to clearly specify the order of operations.
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 );
(“a =” + a + “b=”+b);
}
}
Output
a=5 b=1
The + in the output statement means "string concatenation" in this context, and if necessary, it also performs "string conversion". When the compiler observes that a String is followed by a "+" and the "+" is followed by an element of non-String type, it will try to convert this element of non-String type to String type.
Assignment
The assignment operator "=", its meaning is: take the right value (rvalue) and then copy it to the left (lvalue). An rvalue can be any constant, variable or expression, or any method that can produce values. But the left must be an explicitly named variable. That is to say, there must be a room space that can store the value to the right of the equal sign. For example, a constant can be assigned to a variable
a = 4
But nothing can't be assigned to a constant, and the constant cannot be used as an lvalue 4 = a does not hold true.
The basic type data stores the actual numerical values, rather than referring to an object, so when assigning values to it, it directly talks about the content of one place and copies it to another place. For example, the basic data type uses a = b, and the actual meaning is to copy the content in b to a. If a is assigned a value b later, it will not be affected. But not all assignments will achieve this expected effect.
When doing assignment operations for an object, we actually operate on the reference of the object, so if we assign an object to another object, we actually copy the "reference" from one place to another, which means that while we are the value of one of the objects, the other will also change with this.
class Tank{
int level;
}
public class Assignment{
public static void main(String [] args){
Tank t1=new Tank();
Tank t2=new Tank();
=9;
=47;
(“1: ” ++”,”+);
t1 = t2;
(“2: ” ++”,”+);
=27;
(“3: ” ++”,”+);
}
}
Output
1:: 9,:47;
2:: 47,:47;
3::27,:27;
In this example, the problem of operating references occurs. While we modify t1, t2 is also modified. In most cases, we hope that t1 and t2 can be independent of each other. However, since the assignment operates on an object's reference, t1 and t2 contain the same references here, and they point to the same object (original t1 contains a reference to the object, which is pointing to an object with a value of 9. When t1 is assigned, this reference is overwritten, which means it is lost; and the object that is no longer referenced will be automatically cleaned by the "garbage collector".
This special phenomenon is usually called "aliasing phenomenon", which is a basic way to operate objects in Java. In this example, if you want to avoid alias, now we should directly operate on the object's value:
=;
This way, writing can keep the two objects independent, but directly manipulating the domain of the object can easily lead to confusion and violates the principles of good object-oriented programming.
The following example introduces the alias problem in method calls
class Letter{
char c;
}
public class PassObject{
static void f(Letter y){
='z';
}
public static void main(String [] args){
Letter x=new Letter();
='a';
(“1: ” + );
f(x);
(“2: ” + );
}
}
Output
1: : a
2: : z
When using the f method, we seem to think that it copies a copy of its parameter Letter y within scope; but in fact it is just passing a reference. So the code line
='z';
What actually changes are objects other than f().
Other assignment operators
Operator |
Use |
Equivalent to |
+= |
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 |