[JAVA learning notes] - Operator

Arithmetic operator

Basic four operators: + - * /%

The four operations should be familiar to everyone. It is worth noting that division:

1. The result of int / int can only be int. if you want to get a decimal, you need to borrow the double type to calculate it

For example:

int a = 1;
int b = 2;
System.out.println(a / b);
// The result is 0

//To get 0.5, you have to turn one of them into double
System.out.println((double)a / b);

Operation results:

2. 0 cannot be a divisor

int a = 1;
int b = 0;
System.out.println(a / b)
// Operation results
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:5)

3. % is a remainder sign, which can take modules not only for int, but also for double

System.out.println(11.3 %4);

Operation results:

Compound operator

operatordescribeexample
+=After adding the values of the left operand and the right operand, it is assigned to the left operanda += 1 is equivalent to a = a + 1
-=The left operand is assigned to the left operand after subtracting the value of the left operand from the value of the right operanda -= 1 is equivalent to a = a - 1
*=The left operand is assigned to the left operand after multiplying the value of the left operand by the value of the right operanda *=1 is equivalent to a = a * 1
/=After dividing the left operand by the value of the right operand, the left operand is assigneda /= 1 is equivalent to a = a / 1

Auto increment / Auto decrement operator

++It can be placed before or after operands, such as:

++i;
i++ ;

++i means that i increases by 1 and then participates in other operations;
i + + is that after i participates in the operation, the value of i increases by 1.
Self subtraction operator -- similar to it, it just changes addition to subtraction, so it is not restated.
Example:

int a = 10;
int b = ++a;
System.out.println(b);
int c = a++;
System.out.println(c);

Operation results:

Therefore, if the return value of the expression of auto increment operation is not removed, there is no difference between post auto increment and pre auto increment; If the return value of the expression is removed, the pre auto increment return value is the value after auto increment, and the post auto increment return value is the value before auto increment.

Relational operator

There are six main relational operators:

==(equal to)= (not equal to) < (less than) > (greater than) < = (less than or equal) > = (greater than or equal)
Example:

int a = 10;
int b = 20;
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a < b);
System.out.println(a > b);
System.out.println(a <= b);
System.out.println(a >= b);

Operation results:

From the running results, it can be concluded that the return values of the expressions of relational operators are of boolean type.

Logical operator

&&(logic and)
||(logical or)
! (logical non)

Logic and&&

Rule: both expressions are true and the result is true, otherwise the result is false.
Example:

int a = 10;
int b = 20;
int c = 30;
System.out.println(a < b && b < c);

Operation results:

Logical or||

Rule: both expressions are false and the result is false, otherwise the result is true.

int a = 10;
int b = 20;
int c = 30;
System.out.println(a < b || b < c);

Logical non!

The operand is true and the result is false; The operand is false and the result is true (this is a unary operator with only one operand)

 		int a = 10;
        int b = 20;
        System.out.println(!(a < b));

Operation results:

Short-circuit evaluation

1. For & &, if the expression on the left is false, the return value of the expression is false, and the expression on the right will not be executed.
2. For 𞓜, if the expression on the left is true, the return value of the whole expression is true. Similarly, the expression on the right is not executed.
Example:

System.out.println(10 > 20 && 10 / 0 == 0); // Print false
System.out.println(10 < 20 || 10 / 0 == 0); // Print true

Operation results:

We know that 10 / 0 will report an error in the compiler, but the short-circuit evaluation rule, so the statement is not executed, so the compiler will not report an error.

Bit operation

Bit operation: it is an operation based on binary bits.

Operatorfunction
&Bitwise AND: if both binary bits are 1, the result is 1, otherwise the result is 0.
|Bitwise |: if both binary bits are 0, the result is 0, otherwise the result is 1.
~Reverse by bit ~: if the binary bit is 0, it will be converted to 1, and if it is 1, it will be converted to 0
^Bitwise XOR ^: if the binary bits of two numbers are the same, the XOR is 0 and the difference result is 1.

Shift operation

There are three shift operators in java: (all operate according to binary bits)

<< >> >>>

Shift left<<

Don't use the leftmost bit, and fill 0.5 on the rightmost bit
Example:

 public static void main(String[] args) {
        int a = -20;
        int b = 20;
        System.out.printf("%d\n", a << 1);
        System.out.println((b << 1));
    }

Operation result: equivalent to multiplying by 2

Move right > >

Don't use the far right, and fill the sign bit on the left (positive numbers fill 0 and negative numbers fill 1);
Example:

 		int a = -8;
        int b = 20;
        System.out.printf("%d\n", a >> 1);
        System.out.println((b >> 1));

Operation result: equivalent to dividing 2

Move unsigned right > > >

No more on the far right, and 0 on the far left;

int a = 0xffffffff;
System.out.printf("%x\n", a >>> 1);
// Run results (note that they are printed in hexadecimal)
7fffffff

Tips:

  1. Shift left by 1 bit, equivalent to the original number * 2 Shift N bits to the left, which is equivalent to the nth power of the original number * 2
  2. Shift 1 bit to the right, equivalent to the original number / 2 Shift N bits to the right, which is equivalent to the nth power of the original number / 2
  3. Because the shift efficiency of computer calculation is higher than that of multiplication and division, when a code exactly multiplies and divides to the nth power of 2, it can be replaced by shift operation
  4. Moving negative digits or shifting too many digits is meaningless

Conditional operator (ternary operator)

The only ternary operator in java.
Usage:

Expression 1? Expression 2: expression 3
The expression 1 is a boolean expression. If the expression 1 is true, execute the expression 2. If the expression 1 is false, execute the expression 3.

Example:

//Find the maximum of two integers
int a = 10;
int b = 20;
int max = (a > b) ? a : b;
//Operation result max = 20;

Operator precedence


There is priority between operators We don't have to remember the specific rules Just put parentheses in the code that may be ambiguous

Keywords: Java

Added by nocniagenti on Tue, 04 Jan 2022 04:04:57 +0200