Foundation of JAVA series

Posted by jmcneese on Sun, 19 Dec 2021 06:31:47 +0100

catalogue

6. Java operators

6.1 arithmetic operators

6.2 relational operators

6.3 bit operators

6.4 logical operators

6.5 assignment operators

6.6 conditional operator (?:)

6.7. instanceof operator

6.8 operator priority

Continued above

Foundation of JAVA series (I)

6. Java operators

Java operators are mainly divided into arithmetic operators, relational operators, bit operators, logical operators, assignment operators and other operators.

6.1 arithmetic operators

Arithmetic operator
operatordescribeExample
a = 3 , b = 2
Value of c
+Add operator add on both sidesc = a + b5
-Subtract the right from the left of the minus operatorc = a - b1
*Multiply operator multiplies both sidesc = a * b6
/Divide - Operator left divided by rightc = a / b1
%The remainder operator divides the left side of the operator by the right side and returns the remainderc = a % b1
++Increment - the value of the operand plus 1c = a++
c = ++a
c = 3 , a = 4
c = 5 , a = 5
--Self subtraction - the value of the operand minus 1

c = b--

c= --b

c = 2 , b = 1
c = 0 , b = 0

Other arithmetic operators need two operands to operate, but the self increasing and self decreasing operator only needs one operand.

c = a + + is equivalent to c = a; a = a+1 ;

c = ++a is equivalent to a = a + 1; c = a ;

c = b -- equivalent to c = b; b = b-1 ;

c = --b is equivalent to B = B-1; c = b;

Prefix self increment and self decrement (+ + A, - a): first perform self increment or self decrement operation, and then perform expression operation

Suffix self increment and self decrement (a + +, a --): first perform expression operation, and then perform self increment or self decrement operation

Code demonstration:

public class CSDNTest {

    public static void main(String[] args) {
        int a = 3;
        int b = 2;
        int c ;
        c = a+b;
        System.out.println("c = a + b ,c The value of is: "+c);
        c = a-b;
        System.out.println("c = a - b ,c The value of is: "+c);
        c = a*b;
        System.out.println("c = a * b ,c The value of is: "+c);
        c = a/b;
        System.out.println("c = a / b ,c The value of is: "+c);
        c = a%b;
        System.out.println("c = a % b ,c The value of is: "+c);
        c = a++;
        System.out.println("c = a++ ,c The value of is: "+ c + ", a The value of is:" + a);
        c = ++a;
        System.out.println("c = --a ,c The value of is: "+ c + ", a The value of is:" + a);
        c = b--;
        System.out.println("c = b-- ,c The value of is: "+ c + ", b The value of is:" + b);
        c = --b;
        System.out.println("c = --b ,c The value of is: "+ c + ", b The value of is:" + b);

    }
}

Operation results:

6.2 relational operators

The result of relational operation is boolean value.

Relational operator
operatordescribeExample
a = 2 , b = 3
Operation result
==Equal: whether the left and right sides of the comparison operator are equal. The operation result is a boolean valuea == bfalse
!=Not equal: whether the left and right sides of the comparison operator are not equal. The operation result is a boolean valuea != btrue
>Greater than: check whether the left side of the operator is greater than the right side, and the operation result is a boolean valuea > bfalse
<Less than: check whether the left side of the operator is less than the right side, and the operation result is a boolean valuea < btrue
>=Not less than: check whether the left side of the operator is not greater than the right side, and the operation result is a boolean valuea >= bfalse
<=Not greater than: check whether the left side of the operator is not less than the right side, and the operation result is a boolean valuea <= btrue

Code demonstration:

public class CSDNTest {

    public static void main(String[] args) {
        int a = 2;
        int b = 3;
        boolean c ;
        c = a==b;
        System.out.println("c = a==b ,c The value of is: "+c);
        c = a!=b;
        System.out.println("c = a!=b ,c The value of is: "+c);
        c = a>b;
        System.out.println("c = a>b ,c The value of is: "+c);
        c = a<b;
        System.out.println("c = a<b ,c The value of is: "+c);
        c = a>=b;
        System.out.println("c = a>=b ,c The value of is: "+c);
        c = a<=b;
        System.out.println("c = a<=b ,c The value of is: "+c);
    }
}

Operation results:

6.3 bit operators

Bitwise operators are applied to integer types (int), long, short, char, and byte. Bit operators act on all bits and operate bitwise.

Bitwise Operators
operatordescribeExample
a = 20 =1 0100B
b = 13 =   1101B
Value of c
&And: bitwise and the operands on the left and right sides of the operator. 1 & 1 are 1 and the rest are 0c  = a & b4, i.e. 100B
|Or: bitwise or the operands on the left and right sides of the operator, 0 | 0 is 0, and the rest are 1c  = a & b29, i.e. 11101B
^XOR: bitwise XOR the operands on the left and right sides of the operator. The difference is 1 and the same is 0c  = a & b25, i.e. 11001B
~Negate: negate the operand bit by bit, ~ 1 is 0, ~ 0 is 1c  = ~a-21, i.e. 1111 1111 1111 1111 1111 1110 1011b
<<Shift left n bits by bit: shift the operand to the left n bits by bit, which is equivalent to multiplying the operand by 2^nc = a << 280, i.e. 1010000B
>>Shift the operand to the right by N bits, which is equivalent to dividing the operand by 2^nc = a >> 25, i.e. 101B
>>>Shift n bits to the right by bit, and fill 0 in the empty bit5, i.e. 101B

Operation results:

Signed numbers are stored in the form of complement in the computer. int data accounts for 4 bytes, that is, 32 bits.

6.4 logical operators

Logical operator
operatordescribeExample
a = true , b = false
Operation result
&&Logical and: check whether the operands on the left and right sides of the operator are true. When both operands are true, the operation result is true, otherwise the operation result is falsea && bfalse
||Logical or: check whether the operands on the left and right sides of the operator are true. When both operands are false, the operation result is false; otherwise, the operation result is truea || btrue
!Non: inverts the state of the operand,! True equals false,! False equals true!a
!b
false
true

Code demonstration:

public class CSDNTest {

    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean c ;
        c = a&&b;
        System.out.println("c = a&&b ,c The value of is: "+c);
        c = a||b;
        System.out.println("c = a||b ,c The value of is: "+c);
        c = !a;
        System.out.println("c = !a ,c The value of is: "+c);
        c = !b;
        System.out.println("c = !b ,c The value of is: "+c);
    }
}

Operation results:

When & & the operand on the left is false, the operation result must be false, so whether the operand on the right is true or not is ignored

6.5 assignment operators

Assignment Operators
Operatordescribe

Example
a = 3  , c = 5

Value of c
=Simple assignment operator: assign the value of the right operand to the left operandb = a+58
+ =Addition and assignment operator: assigns the result of the left operand plus the right operand to the left operandc + = a is equivalent to c = c + a8
- =Subtraction and assignment operator: assigns the result of subtracting the left operand from the right operand to the left operandc - = a is equivalent to c = c - a5
* =Multiplication and assignment operator: assigns the result of multiplying the left operand by the right operand to the left operandc * = a is equivalent to c = c * a15
/ =Division and assignment operator: assigns the quotient of the left operand divided by the right operand to the left operandc / = a is equivalent to c = c / a5
%=Modulo and assignment operators: assign the remainder of the left operand divided by the right operand to the left operandC% = a is equivalent to c = c + a2
<< =Left shift assignment operator: assigns the result of shifting the left operand by n bits to the left operandC < < = a is equivalent to C = C < < a16
>> =Right shift assignment operator: assigns the result of shifting the left operand right by n bits to the left operandC > > = a is equivalent to C = C > > a2
&=Bitwise and assignment operator: assigns the result of bitwise and after the left and right operands to the left operandC & = a is equivalent to C = C & A2
^ =Bitwise exclusive or assignment operator: assigns the result of bitwise exclusive or of the left and right operands to the left operandc ^ = a is equivalent to c = c ^ a1
| =Bitwise or assignment operator: assigns a bitwise or subsequent result of the left and right operands to the left operandc | = a is equivalent to c = c | a3

Code demonstration:

public class CSDNTest {

    public static void main(String[] args) {
        int a = 3;
        int b;
        int c = 5;
        System.out.println("a The value of is: "+a);
        System.out.println("c The value of is: "+c);
        b = a+c;
        System.out.println("b = a+c ,b The value of is: "+b);
        c +=a;
        System.out.println("c +=a; ,c The value of is: "+c);
        c -=a;
        System.out.println("c -=a; ,c The value of is: "+c);
        c *=a;
        System.out.println("c *=a; ,c The value of is: "+c);
        c /=a;
        System.out.println("c /=a; ,c The value of is: "+c);
        c %=a;
        System.out.println("c %=a; ,c The value of is: "+c);
        c <<=a;
        System.out.println("c <<=a ,c The value of is: "+c);
        c >>=a;
        System.out.println("c >>=a ,c The value of is: "+c);
        c &=a;
        System.out.println("c &=a ,c The value of is: "+c);
        c ^=a;
        System.out.println("c ^=a ,c The value of is: "+c);
        c |=a;
        System.out.println("c |=a ,c The value of is: "+c);
    }
}

Operation results:

Remember: move the left translation of the equal sign to the right, and the left operand is equal to the expression after moving.

6.6 conditional operator (?:)

The conditional operator, also known as the ternary operator, has three operands. You need to judge whether the value of the first operand is true. If it is true, the operation result is the value of the second operand; If false, the result is the value of the third operand.

Code demonstration:

public class CSDNTest {

    public static void main(String[] args) {
        int a = 3;
        int b = 5;
        int c;
        System.out.println("a The value of is: "+a);
        System.out.println("b The value of is: "+5);
        c = a>b?a:b;
        System.out.println("a and b In, the maximum value is: "+c);
        c = a<b?a:b;
        System.out.println("a and b In, the minimum value is: "+c);
    }
}

Operation results:

6.7. instanceof operator

instanceof operator is a binary operator, which is used to check whether an object instance is an instance of a specific type or its subclass (class type or interface type), and the operation result is a boolean value.

boolean result = object instanceof class;

6.8 operator priority

When multiple operators appear at the same time, the priority of operators is involved.

Order priority from high to low:

Operator priority
categoryOperatorAssociativity
suffix() [] . (point operator)Left to right
one yuanexpr++ expr -- (suffix self increasing and self decreasing)Left to right
one yuan++expr --expr + - ~ ! (prefix self increasing and self decreasing, sign negative, non)Right to left
Multiplicity*/% (multiplication, division and remainder)Left to right
Additive+- (plus and minus)Left to right
Shift< < > > > > > (shift left, shift right and shift right by bit, and fill in 0)Left to right
Relationship>> = < < = (greater than, greater than or equal to, less than or equal to)Left to right
Equal==  != (equal to, not equal to)Left to right
Bitwise ANDLeft to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logic and&&Left to right
Logical or| |Left to right
condition?: Right to left
assignment=  +=  -=  *=  /= %= >>= <<= &=  ^=  |=Right to left
commaLeft to right

Unfinished to be continued...

Topics: Java Back-end