# Learning the basic syntax of java (3)

## java operator

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 operator
- Bitwise Operators
- Logical operator
- Assignment Operators
- Other Operators

## Arithmetic operator

Arithmetic operators are used in mathematical expressions, and their functions are the same as those in mathematics. The following table lists all 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 | describe | example |
---|---|---|

+ | Add - the values on both sides of the add operator | A + B equals 30 |

- | Subtraction - left operand minus right operand | A – B equals - 10 |

* | Values on both sides of the multiply multiply operator | A * B equals 200 |

/ | Division - left operand divided by right operand | B / A equals 2 |

％ | Remainder - the remainder of the left operand divided by the right operand | B%A equals 0 |

++ | Auto increment: the value of the operand increases by 1 | B + + or + + B equals 21 (see the difference below) |

-- | Self subtraction: the value of the operand is reduced by 1 | B -- or -- B equals 19 (see the difference below) |

### example

The following simple example program demonstrates arithmetic operators. Copy and paste the following Java program and save it as test Java file, then compile and run the program:

## example

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--) ); // See 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

### Self increasing and self decreasing operator

1. The self increasing (+ +) and self decreasing (- -) operator is a special arithmetic operator. In the arithmetic operator, two operands are required for operation, and the self increasing and self decreasing operator is an operand.

## example

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

The operation result is:

The value after self increment operation is equal to 4 The value after self subtraction is equal to 2

Resolution:

- int b = ++a; The splitting operation process is: a=a+1=4; b=a=4, the final result is b=4,a=4
- int d = --c; The splitting operation process is: c=c-1=2; d=c=2, the final result is d=2,c=2

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

3. Suffix self increment and self subtraction (a++,a --): first perform expression operation, and then perform self increment or self subtraction operation. Examples:

## 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 self increasing operator a="+a+",x="+x); System.out.println("After suffix operation of autoincrement operator b="+b+",y="+y); } }

The operation result is:

After prefix operation of self increasing operator a=6，x=12 After suffix operation of autoincrement operator b=6，y=10

## Relational operator

The following table shows the relational operators supported by Java

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

operator | describe | example |
---|---|---|

== | Checks if the values of the two operands are equal, and if they are equal, the condition is true. | (A == B) is false. |

!= | Check if the values of the 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 relational operators. Copy and paste the following Java program and save it as test Java file, then compile and run the program:

## Test.java file 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 bitwise operators, which are applied to integer types (int), long, short, char, and byte.

Bitwise operators act on all bits and operate bitwise. Assume a = 60, b = 13; Their binary format 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 | describe | example |
---|---|---|

＆ | If the corresponding bits are all 1, the result is 1, otherwise it is 0 | (A & B), get 12, i.e. 0000 1100 |

| | If the corresponding bits are all 0, the result is 0, otherwise it is 1 | (A | B) get 61, i.e. 0011 1101 |

^ | If the corresponding bit values are the same, the result is 0, otherwise it is 1 | (A ^ B) get 49, i.e. 0011 0001 |

〜 | The bitwise negation operator flips each bit of the operand, that is, 0 becomes 1 and 1 becomes 0. | (~ A) get - 61, i.e. 1100 0011 |

<< | Bitwise shift left operator. The left operand shifts left by bits the number of bits specified by the right operand. | A < < 2 gets 240, i.e. 1111 0000 |

>> | Bitwise shift right operator. The left operand shifts right by bits the number of bits specified by the right operand. | A > > 2 gets 15, i.e. 1111 |

>>> | Bitwise shift right zeroing operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand, and the empty bits obtained by the movement are filled with zeros. | A> > > 2 gets 15, i.e. 0000 1111 |

### example

The following simple example program demonstrates bitwise operators. Copy and paste the following Java program and save it as test Java file, then compile and run the program:

## Test.java file 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 operator

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

Operator | describe | example |
---|---|---|

&& | They are called logical and operators. The condition is true if and only if both operands are true. | (A & & B) is false. |

| | | It is called a logical or operator. If either of the two operands is true, the condition is true. | (a | b) is true. |

! | It is called a logical non operator. Used to reverse the logical state of an operand. If the condition is true, the logical non operator will get false. | ! (A & & B) is true. |

### example

The following simple example program demonstrates logical operators. Copy and paste the following Java program and save it 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 only when both operands are true. However, when the first operation is false, the result must be false. At this time, 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 short circuit logic operator is"+b); System.out.println("a The result is"+a); } }

The operation result is:

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

Analysis: the program uses the short-circuit logic operator (& &). First, if the result of a < 4 is false, then the result of b must be false. Therefore, the second operation, the judgment of a + + < 10, is no longer performed, so the value of a is 5.

## Assignment Operators

The following are the assignment operators supported by the Java language:

Operator | describe | example |
---|---|---|

= | A simple assignment operator that assigns the value of the right operand to the left operand | C = A + B will assign the value obtained by A + B to C |

+ = | The addition assignment operator, which adds the left and right operands and assigns them to the left operand | C + = A is equivalent to C = C + A |

- = | Subtraction and assignment operator, which subtracts the left operand from the right operand and assigns it to the left operand | C - = A is equivalent to C = C - A |

* = | Multiplication and assignment operator, which multiplies the left and right operands and assigns values to the left operand | C * = A is equivalent to C = C * A |

/ = | The division and assignment operator, which divides the left and right operands and assigns them to the left operand | C / = A, C is equivalent to C = C / A when it is of the same type as A |

(％)= | Modulo and assignment operator, which assigns the left and right operands to the left operand after modulo | C% = A is equivalent to C = C% A |

<< = | Left shift assignment operator | C < < = 2 is equivalent to C = C < < 2 |

>> = | Right shift assignment operator | C > > = 2 is equivalent to C = C > > 2 |

＆= | Bitwise and assignment operators | C & 2 is equivalent to C = C & 2 |

^ = | Bitwise XOR assignment operator | C ^ = 2 is equivalent to C = C ^ 2 |

| = | Bitwise or assignment operator | C | 2 is equivalent to C = C | 2 |

### example

The following simple example program demonstrates the assignment operator. Copy and paste the following Java program and save it as test Java file, then compile and run the program:

## Test.java file 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 called ternary operators. The operator has three operands and needs to judge the value of 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 file code:

public class Test { public static void main(String[] args){ int a , b; a = 10; // If a is equal to 1, set b to 20, otherwise it is 30 b = (a == 1) ? 20 : 30; System.out.println( "Value of b is : " + b ); // If a is equal to 10, set b to 20, otherwise it is 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 to check whether the object is a specific type (class type or interface type).

The instanceof operator uses 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 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; // Because name is of String type, it returns true

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

Take 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 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 later? This involves the priority of operators. In a multi operator expression, different operator priorities will lead to very different results.

For example, (1 + 3) + (3 + 2) * 2. If the expression is calculated by the plus sign first, the answer is 18. If the expression is calculated by the multiplier sign first, the answer is 14.

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

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

category | Operator | Relevance |
---|---|---|

suffix | () [] . (point operator) | Left to right |

one yuan | expr++ expr-- | From left to right |

one yuan | ++expr --expr + - ～ ! | Right to left |

Multiplicity | * /％ | Left to right |

Additive | + - | Left to right |

displacement | >> >>> << | Left to right |

relationship | > >= < <= | Left to right |

equal | == != | Left to right |

Bitwise AND | ＆ | Left 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 |

comma | ， | Left to right |