# JAVA operator summary

Posted by Jose Arce on Tue, 08 Mar 2022 23:37:51 +0100

catalogue

Arithmetic operators (+,, *, /)

Modulo / remainder operator (mod)

Self increasing and self decreasing operators (Num + +, Num --, + num, -- Num)

Logical operators (& &, ||,!)

Bit operation (&, |, ^)

< < > > and > > >

Comparison operators (<, >, < =, > =, = =,! =)

 Operator type Symbol Arithmetic operator ' + ',  ' - ',  ' * ',  ' / ' modulus operator ' % ' Self increasing and self decreasing operation num++, num--, --num, ++num Logical operator &&, ||, ! operator &, |, ^ < < > > and > > > displacement <<, >>, >>> Comparison operator <, >, <=, >=, ==, !=

## Arithmetic operators (+,, *, /)

The four operators +, -, *, / in java represent addition, subtraction, multiplication and division respectively

```    public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println(a + b);//13
System.out.println(a - b);//7
System.out.println(a * b);//30
System.out.println(a / b);//3
}```

matters needing attention:

+- it should be noted that the maximum or minimum value of this type should not be exceeded. If you continue to increase the maximum value, the minimum value will be output. Similarly, if you continue to decrease the minimum value, the maximum value will be output

```    public static void main(String[] args) {
int a = Integer.MAX_VALUE;//int Max
int b = Integer.MIN_VALUE;//int min
System.out.println(a);//2147483647
System.out.println(a+1);//-2147483648
System.out.println(b);//-2147483648
System.out.println(b-1);//2147483647
}```

When dividing (/), if there are two ints that cannot be divided by an integer, the decimal point will be ignored and an int type result will be returned. The denominator cannot be 0! When a floating-point type divides by 0, an infinity will be returned. When an integer is used to divide by 0, an error will be reported directly

```    public static void main(String[] args) {
System.out.println(10/3);//3
System.out.println(10.0/3);//3.333 cycle
System.out.println((float)10/3);//3.333 cycle
System.out.println((10/3)*1.0);//3.0
System.out.println(10.0/0);//Infinity (infinity)
System.out.println(10/0);//Direct error reporting
}```

## Modulo / remainder operator (mod)

Modulo is also called remainder. It returns the remainder of the two numbers after division. If the two numbers can be divided by integer, it returns 0. Similarly to the division above, if an integer and 0 modulo will directly report an error, and NaN(Not a Number) will be returned when floating-point and 0 modulo

```    public static void main(String[] args) {
//A condition that cannot be divisible
System.out.println(10%3);//1
System.out.println(-10%3);//-1
System.out.println(10%-3);//1
System.out.println(-10%-3);//-1
System.out.println(10.0%3);//1.0
System.out.println(10.0%3.1);//0.69999
System.out.println(10.0%0);//NaN
//When divisible
System.out.println(10%2);//0
}```

Here is an example of using modulus to judge whether the input number is odd or even

```    public static void main(String[] args) {
//input
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
//Even if it can be divided by 2
if(num % 2 == 0){
System.out.println("even numbers");
}else{
System.out.println("Odd number");
}
}```

## Self increasing and self decreasing operators (Num + +, Num --, + num, -- Num)

Both num + + and + + num represent adding 1 to num, but num + + adds 1 directly after this run, and + + num has added 1 when reading num Such as the following code:

```    public static void main(String[] args) {
int num = 10;
int test = 20;
//num + + does not add 1 when reading
System.out.println(num++);//10
//Here num = 11
//When using + + num, 1 has been added when reading num
//So the operation here is 11 + 1
System.out.println(++num);//12
//Now num = 12
System.out.println(num++ + ++num);//12+14 = 26
//Similarly
System.out.println(--test);//19
System.out.println(test--);//19
System.out.println(test);//18
}```

## Logical operators (& &, ||,!)

&&(logical and) means to return true when both conditions are true, and false if either condition is false

||(logical or) returns true when one or more of the two conditions are true, and false when only two conditions are false

! (logical non) change true to false and false to true

```    public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
//And&&
System.out.println((num1 > num2 && num1 != 0));//False & & true returns false
System.out.println(num1 != 0 && num2 != 0);// True & & true returns true
//Or||
System.out.println((num1 > num2 || num1 != 0));//False | true returns true
System.out.println(num1 == 0 || num2 == 0);//False | false returns false
//No!
System.out.println(!true);//return false
System.out.println(!false);//return true
}```

## Bit operation (&, |, ^)

Bit operation is carried out on the binary stored in the computer. The binary has only 0 and 1

 Operation name Symbol describe example And & Returns 1 when both are 1, otherwise returns 0 00110 & 00100 = 00100 or | When one or more are 1, it returns 1. If both are 0, it returns 0 00110 | 00100 = 00110 XOR ^ When the alignment is different, it returns 1 and 0 in other cases So A^A returns 0 00110 ^ 00100 = 00010
```    public static void main(String[] args) {
int a = 14;//1110 binary
int b = 10;//1010 binary
// 0000 1110(14)
//&0000 1010(10)
// 0000 1010 = 10
System.out.println(a & b);//10
// 0000 1110(14)
//|0000 1010(10)
// 0000 1110 = 14
System.out.println(a | b);//14
// 0000 1110(14)
//^0000 1010(10)
// 0000 0100 = 4
System.out.println(a ^ b);//4
System.out.println(a ^ 14);//0
}```

An algorithm for calculating binary has several 1s:

For example, 0110 has two 1s, so it returns 2

```    public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int cout = 0;
while(n != 0){
n = n & (n-1);
//Count the number of &
cout++;
}
System.out.println(cout);
}
```

## < < > > and > > >

< < operate binary shift left and fill 0 on the far right, > > operate binary shift right

For example, the binary of 14 is 0000 1110 (14)

14 < < 3 represents that the binary of 14 moves three to the left and becomes 0111 0000 (112)

Shift right

Note that the displacement cannot be negative

>>>It is an unsigned shift right operator. When a number is negative, it cannot be used. It will read in the 1 of the sign bit. There is no sign<<<

```    public static void main(String[] args) {
int a = 14;//0000 1110 binary
//     0000 1110(14)
//>>>3 0000 0001(1)
System.out.println(a>>3);//1
System.out.println(-a>>1);//-7
System.out.println(a>>>3);//1
System.out.println(-a>>>3);//536870910
//The int type is stored as 4 bytes and 32 bits
//Therefore, it is illegal to shift left and right more than 31 bits
System.out.println(a<<32);//Error demonstration
//Nor can it be negative
System.out.println(a>>-2);//Error demonstration
}```

## Comparison operators (<, >, < =, > =, = =,! =)

 Operation name Symbol describe less than < Judge whether the number on the left is less than that on the right greater than > Judge whether the number on the left is greater than that on the right Less than or equal to <= Judge whether the number on the left is less than or equal to the number on the right (the positions of the equal sign and the less than sign cannot be interchanged) Greater than or equal to >= Judge whether the number on the left is greater than or equal to that on the right be equal to == Judge whether the two sides of the equal sign are equal, one = is the assignment, and = = is the judgment Not equal to != Returns true when the left is not equal to the right
```    //The result of the comparison operator is bool type
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a == b);//false
System.out.println(a != b);//true
System.out.println(a < b);//true
System.out.println(a > b);//false
System.out.println(a <= b);//true
System.out.println(a >= b);//false
System.out.println(a => b);//Error demonstration
}```

Topics: Java Back-end