Arithmetic operators
Subtraction (+ 、-、 *,/) Don't say it.
Find remainder operator%
Description: The first operand divided by the second operand, and the remaining value after the result of an integer division is the remainder
Note: The result of the redundancy budget is not always an integer, and when the operand is a floating-point number, the result may be a decimal.
double a = 5.2; double B = 3.1; double mod = a%b; SYSTEM.OUT.PRINTLN (mod); // value is 2.1
Self-increment operator + +
The self-increment operator + + has two points:
- + + is the monocular operator can only manipulate one operand
- + + can only operate on variables of numeric type (integer, floating point type)
There are two ways to use the self-increment operator:
- Before the operand, indicates that the operand is added 1, and then participates in other operations
- After the operand, it means to participate in other operations first, and finally add 1 to the operand.
int a = 1; A+ +; System.out.println (a); // value of 2 double B = 5.2; b+ +; System.out.println (b); // value is 6.2 int c = ++a*4; // The value is 12, because A is 2, plus 1 and then by 4 . int d = a++*4; // The value is 12, because a is 3, multiply by 4, assign to D,a and add 1 .
Self-decrement operator--
Usage and increment operators are similar, just minus 1 of the operands
double b =1.2; b--; // 0.19999999999999996, because the double of Java itself is imprecise int a = 5; int c =--a*5; // - int d = c--*5; System.out.println (d); // -
Bitwise operators
Bitwise operators operate on the binary data of the operands, so to get the correct result, the data must be turned into binary before it is understood.
There are 7 middle-bit operators in Java:
- & : Bitwise-AND, binocular operators, which operate on the binary of two operands, and return 1 when both digits are 1.
- | : Bitwise OR, if the binocular operator is 1, returns 1
- ~ : bitwise non, monocular operator, Each bit of the operand's binary number is reversed, i.e. 1 0,0 to 1
- ^ : Bitwise XOR, binocular operator, when two bits are the same, returns 0. Different return 1
- << : Left-shift operator, monocular operator, shift n-bit, equal to 2n
- >> : Right-shift operator, single-mesh operator, shift n-bit, equivalent to 2n
- >>> : Unsigned Right SHIFT, single-mesh operator
int a = 8; int c = a<<2; // 8, left shift and right shift do not change the variable itself // 32, the equivalent of multiplying by 2 of the 2-time Square
Comparison operators
The comparison operator is used to determine the size of a two variable or a constant light, and the comparison result is a Boolean value (True or false), and the comparison operator in Java:
- >: Greater Than
- >=: greater than or equal to
- <: less than
- <=: Less than or equal to
- = =: equals
- ! =: Not equal to
logical operators
A logical operator is used to manipulate a variable or constant of two Boolean values , and the result is a Boolean value that has 6 main logical operators:
- &&: With, binocular operator, two operands are true, is true, otherwise false
- &: does not short-circuit with when the operand is a Boolean value. same function as &&, but no short circuit
- || : Or, the binocular operator returns true whenever one of the operands is true
- | : does not short-circuit or when the operand is a Boolean value. Function with | | Same, but no short circuit
- ! : Non, monocular operand, reverse
- ^: xor, binocular operand, returns false if two operands do not return true at the same time
Trinocular operator
There is only one three-mesh operator in Java: ?: . The format is as follows:
result = (expression)? RESULT1:RESULT2;
Returns RESULT1 if the expression evaluates to True, otherwise returns RESULT2.
int a = 5; int b = 3; int c = a > B? a:b; // 5, if A>b is true, returns a, otherwise returns B
Java Learning Note Four--operator