Java Basics: Assignment Operator and Other Operators

In the previous article, we introduced bitwise operators, and this article introduces assignment operators and other operators.

overview

The so-called assignment operator, as grand as it sounds, is actually =used to assign values ​​to variables.

for example:

int a = 5;

If that's all there is to it, then this article is over! Hahaha~~~

But how could I let you off so easily.

Arithmetic operators, bitwise operators, and shift operators can be combined with assignment operators to simplify arithmetic statements.

combine arithmetic operators

1、+=

x += y is equivalent to x = x + y.

2、-=

x -= y is equivalent to x = x - y.

3、*=

x *= y, equivalent to x = x * y.

4、/=

x /= y is equivalent to x = x / y.

5、%=

x %= y is equivalent to x = x % y.

Combining bitwise operators

1、&=

x &= y is equivalent to x = x & y.

2、|=

x |= y, which is equivalent to x = x | y.

3、^=

x ^= y, equivalent to x = x ^ y.

Combining shift operators

1、<<=

x <<= y,相当于x = x << y。

2、>>=

x >>= y, equivalent to x = x >> y.

3、>>>=

x >>>= y is equivalent to x = x >>> y.

code demo

Above we introduced that the assignment operator combines arithmetic operators, bit operators, and shift operators. Next, let's see in the form of code whether the value changes before and after simplification.

Go directly to the code:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/7 16:43
 * @description:
 */
public class Fourteen {

    public static void main(String[] args) {
        int a1 = 2, b1 = 2;
        int a2 = 4, b2 = 4;
        int a3 = 6, b3 = 6;

        // 组合使用
        a1 += 2;
        a2 &= 4;
        a3 <<= 6;

        // 不组合
        b1 = b1 + 2;
        b2 = b2 & 4;
        b3 = b3 << 6;

        // 结果对比
        System.out.println(a1 + ":" + b1);
        System.out.println(a2 + ":" + b2);
        System.out.println(a3 + ":" + b3);
    }
}

Results of the:

After the demonstration of the code, we can see that the calculated values ​​before and after simplification are the same, indicating that there is no problem with the combined use.

other operators

In addition to the arithmetic operators, comparison operators, conditional operators, shift operators, bitwise operators and assignment operators mentioned in the previous articles, Java also provides the following operators:

operator example explain
?: x ? y : z This is a simplified description of the if-else statement. In the written example, y is executed when x is true, and z is executed when x is false.
[ ] x[0] For array-related descriptions, in the entry example, the index number of the x array represents the array element 0.
. X. Yes A qualified name used to describe an object, an example entry can be thought of as referencing a y instance variable within an x ​​object.
(Parameters Table) methodName(int x) Used to describe the list of parameters a method receives from its caller. The entry example shows that the method receives a parameter x of type int.
(type) (integer)x It is used to convert the value to the specified type, and the entry example means to convert the value x to the int type.
new new x( ) This is the operator used when creating objects and arrays, example = means to create an object of class x.

Speaking of which, all the operators in java are over, let's use a picture to deepen everyone's memory:

The next article will introduce you to Java operator precedence.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132269462