Java operators
Assignment operator
assignment operator
(=): Take the right value (that is,
Right Value), copy it to the left (i.e.
left value)。
The right value can be any constant, variable, or expression (as long as a value can be generated).
The left value must be a definite, named variable.
PublicclassTest {
Public StaticvoidMain (string[] args) {
intnum = 10;
SYSTEM.OUT.PRINTLN ("num =" + num);
num = 20;
SYSTEM.OUT.PRINTLN ("num =" + num);
}
}
Arithmetic operators
Plus (+)、
minus sign (-)、
Multiplication sign (*)、
Division sign (/)、
modulus (%)Java also supports simplified notation for simultaneous operations and assignment operators, as in C + + +.
The following two types of notation are equivalent:
num = num + 20;//Num plus 20, and then assign the value to num
num + = 20;//Num plus 20, and then assign the value to num
Unary operator
unary plus (+)、
unary minus (-)Represents the positive or negative value of a number.intx =-10;//assign a negative number-10 to x
inty = +10;//assign a positive value of 10 to Y, General + omitted
System.out.println ("x =" + x);
System.out.println ("y =" + y);
self-increment and self-reduction operator
The self-increment operator is
++。
The self-subtraction operator is --.
Example: ++a is equivalent to a = a+1;
Both the self-increment and the self-reduction operations can be divided into prefix and suffix.
In the case of self-increment,
intx = 1;
inty = 1;
System.out.println ("x =" + ++x);//prefix type
System.out.println ("y =" + y++);//suffix type
Output:
x = 2
y = 1
This shows that the prefix is incremented first, then into the expression, and the suffix is the first to enter the expression, and then increment the operation itself.
Relational Operators
The relational operator generates a Boolean result that calculates the relationship between the operands ' values.
If the relationship is true, the result of the relational expression is true, and conversely, the result is false.
Relational operators include less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equals (= =) , and Not equal to (! =).
logical operators
Logical operators include with (&&), or (| |) , non-(!), which generates a Boolean value based on the logical relationship of the parameter.
Parentheses operator
The parentheses operator ()can increase the execution precedence of expressions in parentheses.
intRESULT1 = 1 + 2 + 3 * 4-5;
intRESULT2 = (1 + 2 + 3) * 4-5;
System.out.println ("1 + 2 + 3 * 4-5 =" + result1);
System.out.println ("(1 + 2 + 3) * 4-5 =" + result2);
Output:
1 + 2 + 3 * 4-5 = ten
(1 + 2 + 3) * 4-5 =
Bitwise operators
Bitwise and (&), bitwise OR (|), xor (^), inverse (~), Left (<<), right Shift (>>), unsigned Right Shift (>>>)
Ternary operator
The ternary operator is special, and it has three operands. It is in the form of:
(Boolean expression)? value A : value B
If the value of the Boolean expression is true, the result is a value of a, and conversely, the result is a value of B.
Public StaticintGetmaxnum (intXintY) {
return(x >= y)? x:y;
}
PublicStaticvoidMain (string[] args) {
intx = 5;
inty = 20;
System.out.println ("The max of" + x + "and" + Y + "is:" + getmaxnum (x, y));
}
Direct constants
In general, a "direct constant" is used in a program, and the compiler can know exactly what type to generate, but sometimes it is ambiguous.
The suffix character following a direct constant can flag its type.
If uppercase (or lowercase) L , indicates a long type.
In case of uppercase (or lowercase)
FSaid
floatType. In case of uppercase (or lowercase)
DSaid
DoubleType.
The hexadecimal number applies to all integer data types, with a prefix of 0x (or 0X)followed by a 0~9 or case a~f.
octal numbers are represented by prefix 0 and subsequent 0~7 numbers.
Publicclasstest{
PublicStaticvoidMain (String args[]) {
LongL = 123L;//long: L with a suffix of uppercase or lowercase
floatf = 12.3f;//float type: F with a suffix of uppercase or lowercase
DoubleD = 54.345d;//Double type: D with a suffix of uppercase or lowercase
intx = 0xFFFFFFFF;//hexadecimal number: prefix 0x, followed by 0~9,a~f number to indicate
Charc = 017;//octal number: The prefix is 0, followed by a 0~7 number to indicate
}
};
no sizeof operator in Java, unlike C + +
Precedence and associativity of operators
The following table shows the Java operators in order of precedence from highest to lowest:
Level |
Priority level |
Binding nature |
1 |
[ ] . () (function call) |
From left to right |
2 |
! ~ ++ -- + (single operand) – (single operand) () (type conversion) New |
From right to left |
3 |
* / % |
From left to right |
4 |
+ - |
From left to right |
5 |
<< >> >>> |
From left to right |
6 |
< <= > >= instanceof |
From left to right |
7 |
== != |
From left to right |
8 |
& |
From left to right |
9 |
^ |
From left to right |
10 |
| |
From left to right |
11 |
&& |
From left to right |
12 |
|| |
From left to right |
13 |
? : |
From right to left |
14 |
= + = = *=/=%= ^= <<= >>= >>>= |
From right to left |
References
"Thinking in Java"
JAVA base Operator