Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java operator


May 10, 2021 Java


Table of contents


Java operator

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

  • Arithmetic operator
  • The relationship operator
  • Bit operator
  • The logical operator
  • The assignment operator
  • Other operators

Arithmetic operator

Arithmetic operators are used in mathematical expressions, and they act in the same way as they do in mathematics. The following table lists all arithmetic operators.

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

Operator Describe Example
+ Addition - The values on both sides of the addition operator A plus B is equal to 30
- Subtract - Left operans minus right operations A - B equals -10
* Multiplication - The values on both sides of the multiplication operator A . . . B is equal to 200
/ Divide - Divide the left operans by the right operators B / A equals 2
Mode - Divide the left operant by the remaining number of the right operant B%A is equal to 0
++ Self-increase - The value of the operastr increases by 1 The B-plus or the B-plus is equal to 21
-- Self-detract - The value of the operanciation is reduced by 1 B-- or --B equals 19

Instance

The following simple sample program demonstrates arithmetic operators. Copy and paste the Java program below and save it .java Test, 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--) );
     // 查看  d++ 与 ++d 的不同
     System.out.println("d++   = " +  (d++) );
     System.out.println("++d   = " +  (++d) );
  }
} 

The above examples compile and run 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

The relationship operator

The following table is a Java-supported relationship operator

The value of instance integer variable A in the table is 10 and the value of variable B is 20:

Operator Describe Example
== Check that if the values of the two operasts are equal, the condition is true if they are equal. The (A-B) is false (non-true).
!= Check that if the values of the two operasts are equal, the condition is true if the values are not equal. (A!-B) is true.
> Check that the value of the left operance is greater than the value of the right operast? (A.gt; B) is not true.
< Check that the value of the left operance is less than the value of the right operans, if so the condition is true. (A -lt;B) is true.
> = Check that the value of the left operance is greater than or equal to the value of the right operans, if so the condition is true. (A-gt; s-B) is false.
<= Check that the value of the left operance is less than or equal to the value of the right operance, if so the condition is true. (A -lt;?B) is true.

Instance

The following simple sample program demonstrates a relationship operator. Copy and paste the Java program below and save it .java Test, then compile and run the program:

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 above examples compile and run as follows:

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

Bit operator

Java defines bit operators that apply to types such as integer type (int), long type (long), short type (short), character type (char), and byte type.

Bit operators act on all bits and are calculated by bit. S uppose a is 60, and b is 13; Their binary format represents the following:

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 the bit operator, assuming that the value of the integer variable A is 60 and the value of variable B is 13:

Operator Describe Example
By bit and operator, the bit of the result is 1 when and only if one of the two operans is not 0. (A-B), get 12, i.e. 0000 1100
| By bit or operator, as long as one of the two operans has a non-0 result, the bit is 1. (A | B) get 61, or 0011 1101
^ By bit difference or operator, the bit of the result is 1 when one of the two operans is not the same. (A- B) gets 49, or 0011 0001
Flip each bit of the operastr by bit. (-A) gets -61, i.e. 1100 0011
<< Move the operator to the left by bit. The number of bits specified by the left-shift right-hand operans. A slt;lt;2 gets 240, or 1111 0000
>> Shift the operator to the right by bit. The number of bits specified by the left operans by bit-right shift to the right-hand operans. A .gt;2 gets 15 or 1111
>>> Move the zero operator by bit right. The value of the left operans moves to the right by the number of digits specified by the right operans, and the resulting empty spaces are filled with zeros. A.gt;gt;2 gets 15,0000 1111

Instance

The following simple sample program demonstrates bit operators. C opy and paste the Java program below and save it .java Test, then compile and run the program:

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;     /* 215 = 1111 */
     System.out.println("a >> 2  = " + c );
  
     c = a >>> 2;     /* 215 = 0000 1111 */
     System.out.println("a >>> 2 = " + c );
  }
} 

The above examples compile and run as follows:

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

The logical operator

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

Operator Describe Example
&& It is called logic and operator. The condition is true when and only if both operasts are true. (A and B) is false.
| | It is called a logic or operator. If neither of the two operasts is true, the condition is true. (A | | B) for the truth.
It is called a logical non-operator. T he logical state used to reverse the operastr. If the condition is true, the logical non-operator will get false. (A and B) is true.

Instance

The following simple sample program demonstrates a logical operator. Copy and paste the Java program below and save it .java Test, then compile and run the program:

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 above examples compile and run as follows:

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

The assignment operator

Here are the assignment operators supported by the Java language:

Operator Describe Example
= A simple assignment operator that assigns the value of the right operans to the left-hand operans C - A - B will assign the value obtained by A - B to C
+ = Add and assign operators, which add the left and right operans to the left operastr The equivalent of C , A .
- = The minus and assignment operators, which subtract the left and right operans to the left operans C - - A equivalent to C - C - C -
A
* = Multiply and assign operators, which multiply left and right operans to left operans C . . .
/ = Divide and assign operators, which divide the left and right operans and assign values to the left operans C / - A is equivalent to C - C / A
(%)= The mode-taking and assignment operators, which assign the left and right operans to the left operastr after they are molded C%-A is equivalent to C-C%A
<< = The left shift assignment operator C is equivalent to C, C, and 2 is equivalent to C, C, slt; 2
>> = Shift the assignment operator to the right C sgt; s 2 is equivalent to C s
&= Bit-by-bit and assignment operators The equivalent of C. 2 is equivalent to C
^ = By bit or assignment operator C s 2 is equivalent to C s C s 2
| = A bit-by-bit or assignment operator C | 2 is equivalent to C , C | 2

Instance

A simple sample program for faces demonstrates assignment operators. Copy and paste the Java program below and save it .java Test, then compile and run the program:

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 >>= a = " + 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 above examples compile and run 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 thyme operators. T he operator has three operasts and needs to judge the value of the Boolean expression. The main reason for this operator is to decide which value should be assigned to the variable.

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

Instance

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

The above examples compile and run as follows:

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

The instanceof operator

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

The instanceof operator is in the following format:

( 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/interface on the right side of the operator, the result is true.

Here's an example:

String name = 'James';
boolean result = name instanceof String; // 由于name是Strine类型,所以返回真

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

Take a look at the following 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 above examples compile and run as follows:

true

Java operator priority

When multiple operators appear in an expression, who comes first and who comes after? T his raises the question of the operator's priority. In a multi-operator expression, different operator priorities can result in very different results.

For example, the answer to this expression is 18 if it is evaluated as the plus sign, and 14 if it is given the most priority by the multiplier.

For example, x s 7 s 3 s 2; H ere x gets 13 instead of 20, because the multiplication operator has a higher priority than the addition operator, so it is calculated that 3 x 2 gets 6, and then adds 7.

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

Category Operator Relevance
Suffix () [] . (point operator) Left to right
One dollar + + - ! Right to left
Multiplicity * /% Left to right
Added + - Left to right
Shift >> >>>  << Left to right
Relationship >> = << = Left to right
Equal ==  != Left to right
By bit with Left to right
By bit or ^ Left to right
By bit or | Left to right
Logic and && Left to right
Logic or | | Left to right
Conditions Right to left
Assignment = + = - = * = / =%= >> = << =&= ^ = | = Right to left
Comma Left to right