📜  Java赋值运算符与示例

📅  最后修改于: 2022-05-13 01:55:10.903000             🧑  作者: Mango

Java赋值运算符与示例

运算符构成任何编程语言的基本构建块。 Java也提供了许多类型的运算符,可以根据执行各种计算和功能的需要使用,如逻辑、算术、关系等。它们根据它们提供的功能进行分类。

运算符类型:

  1. 算术运算符
  2. 一元运算符
  3. 赋值运算符
  4. 关系运算符
  5. 逻辑运算符
  6. 三元运算符
  7. 位运算符
  8. 移位运算符

本文解释了有关赋值运算符的所有知识。

赋值运算符

这些运算符用于为变量赋值。赋值运算符的左侧操作数是一个变量,而赋值运算符符的右侧操作数是一个值。右侧的值必须与左侧操作数的数据类型相同。否则,编译器会报错。这意味着赋值运算符具有从右到左的关联性,即运算符符右侧给出的值被分配给左侧的变量。因此,右手边的值必须在使用前声明或应该是一个常数。赋值运算符的一般格式是,

variable operator value;

Java中赋值运算符的类型

赋值运算符通常有两种类型。他们是:

1. 简单赋值运算符:简单赋值运算符与“=”号一起使用,其中左侧由操作数组成,右侧由值组成。右侧的值必须与左侧定义的数据类型相同。

2. 复合赋值运算符:复合运算符用于 +、-、* 和 / 与 =运算符一起使用的地方。

让我们看看每个赋值运算符以及它们是如何操作的:

1. (=)运算符:

这是最直接的赋值运算符,用于将右边的值赋给左边的变量。这是赋值运算符及其功能的基本定义。

句法:

num1 = num2;

例子:

a = 10;
ch = 'y';
Java
// Java code to illustrate "=" operator
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
        // Declaring variables
        int num;
        String name;
 
        // Assigning values
        num = 10;
        name = "GeeksforGeeks";
 
        // Displaying the assigned values
        System.out.println("num is assigned: " + num);
        System.out.println("name is assigned: " + name);
    }
}


Java
// Java code to illustrate "+="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Adding & Assigning values
        num1 += num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}


Java
// Java code to illustrate "-="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Subtracting & Assigning values
        num1 -= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}


Java
// Java code to illustrate "*="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Multiplying & Assigning values
        num1 *= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}


Java
// Java code to illustrate "/="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 20, num2 = 10;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Dividing & Assigning values
        num1 /= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}


Java
// Java code to illustrate "%="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 5, num2 = 3;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Moduling & Assigning values
        num1 %= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}


输出
num is assigned: 10
name is assigned: GeeksforGeeks

2. (+=)运算符:

该运算符是 '+' 和 '='运算符的组合。它通过将左侧变量的当前值与右侧的值相加,然后将结果分配给左侧的操作数来进行操作。

句法:

num1 += num2;

例子:

a += 10

This means,
a = a + 10

Java

// Java code to illustrate "+="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Adding & Assigning values
        num1 += num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}
输出
num1 = 10
num2 = 20
num1 = 30

3. (-=)运算符:

该运算符是 '-' 和 '='运算符的组合。它通过从左侧变量的当前值中减去右侧变量的值,然后将结果分配给左侧的操作数来进行操作。

句法:

num1 -= num2;

例子:

a -= 10

This means,
a = a - 10

Java

// Java code to illustrate "-="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Subtracting & Assigning values
        num1 -= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}
输出
num1 = 10
num2 = 20
num1 = -10

4. (*=)运算符:

该运算符是 '*' 和 '='运算符的组合。它通过将左侧变量的当前值乘以右侧的值,然后将结果分配给左侧的操作数来进行操作。

句法:

num1 *= num2;

例子:

a *= 10
This means,
a = a * 10 

Java

// Java code to illustrate "*="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 10, num2 = 20;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Multiplying & Assigning values
        num1 *= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}
输出
num1 = 10
num2 = 20
num1 = 200

5. (/=)运算符:

该运算符是 '/' 和 '='运算符的组合。它的操作是将左侧变量的当前值除以右侧的值,然后将商分配给左侧的操作数。

句法:

num1 /= num2;

例子:

a /= 10
This means,
a = a / 10

Java

// Java code to illustrate "/="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 20, num2 = 10;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Dividing & Assigning values
        num1 /= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}
输出
num1 = 20
num2 = 10
num1 = 2

6. (%=)运算符:

该运算符是 '%' 和 '='运算符的组合。它的操作是将左侧变量的当前值除以右侧的值,然后将余数分配给左侧的操作数。

句法:

num1 %= num2;

例子:

a %= 3

This means,
a = a % 3

Java

// Java code to illustrate "%="
 
import java.io.*;
 
class Assignment {
    public static void main(String[] args)
    {
 
        // Declaring variables
        int num1 = 5, num2 = 3;
 
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
 
        // Moduling & Assigning values
        num1 %= num2;
 
        // Displaying the assigned values
        System.out.println("num1 = " + num1);
    }
}
输出
num1 = 5
num2 = 3
num1 = 2