The operator

Operators are used to concatenate values, and Java provides arithmetic and logical operators as well as mathematical functions.

Arithmetic operators

1. Common arithmetic operators

The operator logo
Add operation +
Reduce operation
By computing *
In addition to the operation /
Modulus operation %

2. Precautions

① Division operation

If both operands are integers, the result of integer division will be an integer, leaving out the remainder. If one of the operands is floating-point, the result will also be floating-point

    int num1 = 1;
    int num2 = 3;
    double num3 = 1.0;
    double num4 = 3.0;
    float num5 = 1.0 f;
    float num6 = 3.0 f;
    System.out.println(num1 / num2);/ / 0
    System.out.println(num1 / num4);/ / 0.3333333333333333
    System.out.println(num1 / num6);/ / 0.33333334
    System.out.println(num3 / num2);/ / 0.3333333333333333
    System.out.println(num3 / num4);/ / 0.3333333333333333
    System.out.println(num3 / num6);/ / 0.3333333333333333
    System.out.println(num5 / num2);/ / 0.33333334
    System.out.println(num5 / num4);/ / 0.3333333333333333
    System.out.println(num5 / num6);/ / 0.33333334
Copy the code

(2) in addition to zero

An integer divided by 0 yields an exception, while a floating-point number divided by 0 yields an infinity or NaN

        //System.out.println(12 / 0); / / Java. Lang. ArithmeticException: / by zero
        int n1 = 12;
        int n2 = 0;
        double n3 = 12.0;
        //System.out.println(n1 / n2); / / Java. Lang. ArithmeticException: / by zero
        //System.out.println(n3 / n2); //Infinity

        //int n4 = n1 / n2; / / Java. Lang. ArithmeticException: / by zero
        //System.out.println(n4);

        double n5 = n3 / n2;
        System.out.println(n5);//Infinity
Copy the code

③ On modular operation

        int x = 12;
        double x1 = 12.0;
        int y = 5;
        double z = 5.0;
        double z1 = 4.0;
        System.out.println(x % y);/ / 2
        System.out.println(x % z);/ / 2.0
        System.out.println(x % z1);/ / 0.0
        System.out.println(x1 % y);/ / 2.0
Copy the code

Second, mathematical functions

The Math class contains a wide variety of mathematical functions.

1. Common mathematical functions

function format instructions
Take the square root of the number Math.sqrt(x) The result returns a double
Power operation Math.pow(x,a) Two double arguments, resulting in a return value of double
Trigonometric functions Math.sin/math. cos/math. tan, etc
Exponential function Math.exp / Math.log / Math.log10

2. Common mathematical values

constant format instructions
pai Math.PI Provides the closest approximation of this constant
e Math.E Provides the closest approximation of this constant

To avoid adding the Math prefix every time, add import static java.lang.math.* at the top of the source file

The combination of assignment and operator

X = x + 4; X = x + 4; X = x + 4; That’s the same thing as x plus is equal to 4x plus is equal to 4x plus is equal to 4

Note: If the type is different from the type of the left-hand operand, a cast occurs. If x is an int x += 3.5; Is legal, which is equivalent to (int)(x + 3.5).

Relational operators

① == and! =

Detection of equality

② Four commonly used relational operators

< > < = > =

③ Logical operation

&& | |! < br > note: && operation and | | operation is in accordance with the “short circuit” way to evaluate:, that is, if the first operand has been able to determine the value of the expression, then the second operation don’t have to do the operation again.

④ Ternary operators

condition ? expression1 : expression2

Five, bit operators

When working with integer types, you can operate directly on the bits that make up the integer. This means that you can use the mask technique to get bits from integers, which is one of the fastest operators. An operator include four: the & (and) | (or) ^ (xor) ~ (not)

These operators are treated in bitwise mode. >> and << can shift bitwise modes left and right >>>>>>>>> operators fill the highest bit with 0, and >> fills the highest bit with sign bits. The << operator does not exist