[JavaSE]: 7. Java operator

Keywords: Java less

Java operators

One of the most basic uses of computers is to perform mathematical operations. As a computer language, Java also provides a rich set of operators to manipulate variables. We can divide operators into the following groups:

  • Arithmetic operator
  • Relational operators
  • Bitwise Operators
  • Logical operators
  • Assignment Operators
  • Other Operators

Arithmetic operator

Arithmetical operators are used in mathematical expressions, and their functions are the same as those in mathematics. The following table lists all the arithmetic operators.

The example in the table assumes that the value of integer variable A is 10, and the value of variable B is 20:

Operator description example
 +The value A + B on both sides of the add add operator is equal to 30
 -Subtraction - left operand minus right operand A – B equals - 10
 *Multiplication - the value A * B on both sides of the multiplication operator is equal to 200
 /Division - left operand divided by right operand B / A equals 2
 %Remainder - left operand divided by right operand remainder B%A equals 0
 ++Auto increment: the value of the operand increases by 1 B + + or + + B equals to 21 (see the difference below)
--Self decrement: the value of the operand decreases by 1 B -- or -- B equals to 19 (see the difference below)

example

The following simple example program demonstrates the arithmetic operators. Copy and paste the following Java program and save as Test.java File, then compile and run the program:

public class Test {
 
  public static void main(String[] args) {
     int a = 10;
     int b = 20;
     int c = 25;
     int d = 25;
     System.out.println("a + b = " + (a + b) );
     System.out.println("a - b = " + (a - b) );
     System.out.println("a * b = " + (a * b) );
     System.out.println("b / a = " + (b / a) );
     System.out.println("b % a = " + (b % a) );
     System.out.println("c % a = " + (c % a) );
     System.out.println("a++   = " +  (a++) );
     System.out.println("a--   = " +  (a--) );
     // View the difference between d + + and + + d
     System.out.println("d++   = " +  (d++) );
     System.out.println("++d   = " +  (++d) );
  }
}

The compilation and operation results of the above examples are as follows:

a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++   = 10
a--   = 11
d++   = 25
++d   = 27

Auto increment and auto decrement operator

1. The self increasing (+ +) self decreasing (–) operator is a special arithmetic operator, in which two operands are needed to operate, while the self increasing and self decreasing operator is a operand.

example

public class selfAddMinus{
    public static void main(String[] args){
        int a = 3;//Define a variable;
        int b = ++a;//Autoincrement operation
        int c = 3;
        int d = --c;//Self subtraction
        System.out.println("The value after auto increment is equal to"+b);
        System.out.println("The value after subtraction is equal to"+d);
    }
}

The operation result is:

The value after the auto increment operation is equal to 4
 The value after subtraction is equal to 2

Resolution:

  • int b = ++a; the splitting process is: a=a+1=4; b=a=4, and the final result is b=4,a=4

  • int d = --c; the splitting process is: c=c-1=2; d=c=2, and the final result is d=2,c=2

2. Prefix auto increment and auto subtraction (+ + A, – a): first carry out auto increment or auto decrement operation, and then carry out expression operation.

**3. Suffix auto increment and auto subtraction (a++,a –): * * expression operation first, then auto increment or auto decrement operation example:

public class selfAddMinus{
    public static void main(String[] args){
        int a = 5;//Define a variable;
        int b = 5;
        int x = 2*++a;
        int y = 2*b++;
        System.out.println("After prefix operation of autoincrement operator a="+a+",x="+x);
        System.out.println("After the suffix operation of the autoincrement operator b="+b+",y="+y);
    }
}

The operation result is:

a=6, x=12 after prefix operation
 Auto increment operator postfix b=6, y=10

Relational operators

The following table is Java supported relational operators

In the table, the value of instance integer variable A is 10, and the value of variable B is 20:

Operator description example
 ==Checks if the values of two operands are equal, and if they are, the condition is true. (A == B) is false.
! = check if the values of two operands are equal, and if the values are not equal, the condition is true. (a! = b) is true.
>Check whether the value of the left operand is greater than the value of the right operand. If so, the condition is true. (a > b) is false.
< check whether the value of the left operand is less than the value of the right operand. If so, the condition is true. (a < b) is true.
>=Check whether the value of the left operand is greater than or equal to the value of the right operand. If so, the condition is true. (a > = b) is false.
< = check whether the value of the left operand is less than or equal to the value of the right operand. If so, the condition is true (a < = b) is true.

example

The following simple example program demonstrates the relational operator. Copy and paste the following Java program and save as Test.java File, then compile and run the program:

Test.java Document code:

public class Test {
 
  public static void main(String[] args) {
     int a = 10;
     int b = 20;
     System.out.println("a == b = " + (a == b) );
     System.out.println("a != b = " + (a != b) );
     System.out.println("a > b = " + (a > b) );
     System.out.println("a < b = " + (a < b) );
     System.out.println("b >= a = " + (b >= a) );
     System.out.println("b <= a = " + (b <= a) );
  }
}

The compilation and operation results of the above examples are as follows:

a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false

Bitwise Operators

Java defines bit operators, which are applied to integer type (int), long type (long), short type (short), char type (char), byte type (byte) and other types.

Bit operators act on all bits and operate on bits. Suppose a = 60, b = 13; their binary representation will be as follows:

A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
~A= 1100 0011

The following table lists the basic operations of bit operators, assuming that the value of integer variable A is 60 and the value of variable B is 13:

Operator description example
 If the corresponding bits are all 1, the result is 1, otherwise 0 (A & B), and 12, i.e. 0000 1100
 |If the corresponding bits are all 0, then the result is 0, otherwise 1 (A | B) will get 61, that is, 0011 1101
 ^If the corresponding bit values are the same, the result is 0, otherwise 1 (A ^ B) will get 49, that is, 0011 0001
 ~ bitwise negation operator flips each bit of an operand, that is, 0 becomes 1, and 1 becomes 0. (~ A) get - 61, i.e. 11000011
 < bitwise shift left operator. The number of bits specified by the shift left operand to the right operand. A < 2 gives 240, i.e. 1111 0000
 >>Bitwise shift right operator. Moves the left operand right by bits the number of bits specified by the right operand. A > > 2 gets 15, i.e. 1111
 >>>Shift bitwise right to zero operator. The value of the left operand is shifted to the right according to the number of digits specified by the right operand, and the resulting space is filled with zero. A > >

example

The following simple example program demonstrates the bit operator. Copy and paste the following Java program and save as Test.java File, then compile and run the program:

Test.java Document code:

public class Test {
  public static void main(String[] args) {
     int a = 60; /* 60 = 0011 1100 */ 
     int b = 13; /* 13 = 0000 1101 */
     int c = 0;
     c = a & b;       /* 12 = 0000 1100 */
     System.out.println("a & b = " + c );
 
     c = a | b;       /* 61 = 0011 1101 */
     System.out.println("a | b = " + c );
 
     c = a ^ b;       /* 49 = 0011 0001 */
     System.out.println("a ^ b = " + c );
 
     c = ~a;          /*-61 = 1100 0011 */
     System.out.println("~a = " + c );
 
     c = a << 2;     /* 240 = 1111 0000 */
     System.out.println("a << 2 = " + c );
 
     c = a >> 2;     /* 15 = 1111 */
     System.out.println("a >> 2  = " + c );
  
     c = a >>> 2;     /* 15 = 0000 1111 */
     System.out.println("a >>> 2 = " + c );
  }
} 

The compilation and operation results of the above examples are as follows:

a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2  = 15
a >>> 2 = 15

Logical operators

The following table lists the basic operations of logical operators, assuming that boolean variable A is true and variable B is false

Operator description example
 &&Called logic and operator. The condition is true if and only if both operands are true. (A & & B) is false.
|| is called logic or operator. If either of the two operands is true, the condition is true. (a | b) is true.
! Called logical non operator. Used to reverse the logical state of an operand. If the condition is true, the logical non operator gets false. 	! (A & & B) is true.

example

The following simple example program demonstrates the logical operators. Copy and paste the following Java program and save as Test.java File, then compile and run the program:

example:

public class Test {
  public static void main(String[] args) {
     boolean a = true;
     boolean b = false;
     System.out.println("a && b = " + (a&&b));
     System.out.println("a || b = " + (a||b) );
     System.out.println("!(a && b) = " + !(a && b));
  }
}

The compilation and operation results of the above examples are as follows:

a && b = false
a || b = true
!(a && b) = true

Short circuit logic operator

When the and logical operator is used, the result is true when both operands are true, but when the first operation is false, the result must be false, and then the second operation will not be judged.

example:

public class LuoJi{
    public static void main(String[] args){
        int a = 5;//Define a variable;
        boolean b = (a<4)&&(a++<10);
        System.out.println("The result of using the short circuit logic operator is"+b);
        System.out.println("a The result is"+a);
    }
}

The operation result is:

The result of using the short circuit logic operator is false
 a results in 5

Resolution: the program uses the short circuit logical operator (& &), first, judge that the result of a < 4 is false, then the result of b must be false, so the second operation a + + < 10 is no longer executed, so the value of a is 5.

Assignment Operators

The following are assignment operators supported by the Java language:

Operator description example
 =Simple assignment operator, assign the value of the right operand to the left operand C = A + B assign the value of A + B to C
 += addition and assignment operator, which adds left and right operands and assigns them to left operands. C + = A is equivalent to C = C + A
 -= subtraction and assignment operator, which assigns subtraction of left and right operands to left operands C - = A is equivalent to C = C - A
 *= multiplication and assignment operator, which assigns left and right operands multiplication to left operands C * = A equivalent to C = C * A
 /= division and assignment operators, which divide left and right operands and assign them to left operands C / = A, which is equivalent to C = C / A when C and A are of the same type
 (%) = modulo and assignment operator, which assigns left and right operands to left operands after modulo. C% = A is equivalent to C = C% A
 The assignment operator C < < 2 is equivalent to C = C < 2
 >>= right shift assignment operator c > > = 2 is equivalent to C = C > > 2
 The bitwise and assignment operators C & 2 are equivalent to C = C & 2
 ^= bitwise exclusive or assignment operator C ^ = 2 is equivalent to C = C ^ 2
 |= bitwise or assignment operator c | 2 is equivalent to C = C | 2

example

The simple example program of face demonstrates the assignment operator. Copy and paste the following Java program and save as Test.java File, then compile and run the program:

Test.java Document code:

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 0;
        c = a + b;
        System.out.println("c = a + b = " + c );
        c += a ;
        System.out.println("c += a  = " + c );
        c -= a ;
        System.out.println("c -= a = " + c );
        c *= a ;
        System.out.println("c *= a = " + c );
        a = 10;
        c = 15;
        c /= a ;
        System.out.println("c /= a = " + c );
        a = 10;
        c = 15;
        c %= a ;
        System.out.println("c %= a  = " + c );
        c <<= 2 ;
        System.out.println("c <<= 2 = " + c );
        c >>= 2 ;
        System.out.println("c >>= 2 = " + c );
        c >>= 2 ;
        System.out.println("c >>= 2 = " + c );
        c &= a ;
        System.out.println("c &= a  = " + c );
        c ^= a ;
        System.out.println("c ^= a   = " + c );
        c |= a ;
        System.out.println("c |= a   = " + c );
    }
}

The compilation and operation results of the above examples are as follows:

c = a + b = 30
c += a  = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a  = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a  = 0
c ^= a   = 10
c |= a   = 10

Conditional operator (?:)

Conditional operators are also known as ternary operators. The operator has three operands and needs to determine the value of the Boolean expression. The main purpose of this operator is to determine which value should be assigned to the variable.

variable x = (expression) ? value if true : value if false

example

Test.java Document code:

public class Test {
   public static void main(String[] args){
      int a , b;
      a = 10;
      // If a equals 1, set b to 20, otherwise 30
      b = (a == 1) ? 20 : 30;
      System.out.println( "Value of b is : " +  b );
 
      // If a equals 10, set b to 20, otherwise 30
      b = (a == 10) ? 20 : 30;
      System.out.println( "Value of b is : " + b );
   }
}

The compilation and operation results of the above examples are as follows:

Value of b is : 30
Value of b is : 20

instanceof operator

This operator is used to manipulate an object instance and check whether the object is of a specific type (class type or interface type).

The format of instanceof operator is as follows:

( Object reference variable ) instanceof  (class/interface type)

If the object referred to by the variable on the left side of the operator is an object of the class or interface on the right side of the operator, the result is true.

Here is an example:

String name = "James";
boolean result = name instanceof String; // Return true because name is of String type

If the object being compared is compatible with the right type, the operator still returns true.

Here's an example:

class Vehicle {}
 
public class Car extends Vehicle {
   public static void main(String[] args){
      Vehicle a = new Car();
      boolean result =  a instanceof Car;
      System.out.println( result);
   }
}

The compilation and operation results of the above examples are as follows:

true

Java operator priority

When multiple operators appear in an expression, who comes first and who comes next? This involves the priority of operators. In a multi operator expression, different operator priorities can lead to very different results.

For example, (1 + 3) + (3 + 2) * 2, if the expression is calculated by the plus sign with the highest priority, the answer is 18; if the expression is calculated by the multiplier sign with the highest priority, the answer is 14.

For example, x = 7 + 3 * 2; here x gets 13 instead of 20, because the multiplication operator has a higher priority than the addition operator, so first calculate 3 * 2 to get 6, and then add 7.

The operators with the highest priority in the following table are at the top of the table, and the operators with the lowest priority are at the bottom of the table.

Category operator relevance
 Suffix () []. (dot operator) left to right
 Unary expr++ expr -- left to right
 One variable + + expr --expr + - ~! Right to left
 Multiplicative * /% left to right
 Additive + - left to right
 Shift > >
Relation > >
Equal = =! = left to right
 Bitwise AND & left to right
 Bitwise exclusive or ^ left to right
 Press bit or| left to right
 Logic and & & left to right
 Logical or | left to right
 Conditions? : right to left
 Assignment = + = - = * = / =% = > = < = & = ^ = | = right to left
 Comma, left to right

Posted by jebster on Fri, 12 Jun 2020 22:50:59 -0700