首页  >  文章  >  Java  >  Java 中的赋值运算符

Java 中的赋值运算符

WBOY
WBOY原创
2024-08-30 15:19:57863浏览

Java 赋值运算符分为两类,例如简单赋值运算符和复合赋值运算符。顾名思义,赋值运算符用于将值分配给参与运算的变量。简单的赋值运算符处理简单、不复杂的运算,例如加法、减法、乘法和除法。当代码中需要较多的逻辑运算时,使用复合赋值运算符,如 ^、&、%、<>、>>、

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

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

  • 简单赋值运算符
  • 复合赋值运算符

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

复合运算符用于 +、-、* 和 / 与 = 运算符一起使用的情况。

赋值运算符的类型

有各种任务运算符。赋值后将产生目标/目标变量的值。

简单赋值运算符

首先,我们将在 Java 程序的帮助下查看并检查简单赋值运算符的工作情况。该程序包括将两个值分配给数字 1 和数字 2,然后将其打印在输出中以显示这些值已分配给数字。

代码:

class AssignmentOperator
{
public static void main(String[] args)
{
int n1, n2;
// Assigning 5 to number1
n1 = 5;
System.out.println(n1);
// Assigning value of variable number2 to number1
n2 = n1;
System.out.println(n2);
}
}

当我们执行 print 语句时,我们得到以下输出。我们可以看到前面初始化的两个数字已经被打印出来了。这两个数字都初始化为值 5。

输出:

Java 中的赋值运算符

复合赋值运算符

在这里,我们将检查复合赋值运算符的工作情况。以下是复合赋值运算符的列表。

  • += 复合附加赋值运算符
  • -= 复合减法赋值运算符
  • *= 复合乘法赋值运算符
  • /= 复合除法赋值运算符。

上面提到的是存在的四个基本复合赋值运算符。还有其他复合赋值运算符,例如:

  • %= 复合模赋值运算符。
  • &= 复合按位赋值运算符。
  • ^= 复合按位 ^ 赋值运算符。
  • >>= 复合右移赋值运算符。
  • >>>= 复合右移填充 0 赋值运算符。

在本文中,我们将详细检查前四个复合赋值运算符以及其他运算符。复合赋值运算符的基本优点是它可以在 Java 语言程序中节省大量代码。

1.复合附加赋值运算符

该运算符用于在整个循环中连续向变量添加数字。我们将看到一个程序,其中我们借助循环求出第 i 个自然数的和。在 for 循环中,我们将使用复合附加运算符。

代码:

//Program to print the sum uptil ith natural number
import java.io.*;
public class Main
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number upto which you want to find the sum");//Print statement
int i=Integer.parseInt(br.readLine());//Taking input from user
int sum=0;//Initializing sum=0
//Beginning of for loop
for (int j=1; j<i; j++)
{
sum+= j;//Compound assignment operator being used here
}//end of for loop
System.out.println("Sum of first " +i+ " natural numbers = " +sum);
}// end of main
}// end of class

我们看到,当我们输入值为10时,即得到前10个自然数的和,如45。

输出:

Java 中的赋值运算符

2.复合减法赋值运算符

该程序可用于从现有的较大数字中删除一个数字。在下一个程序中,我们将看到从更大的数字 100 中删除数字。

代码:

//Program to print the sum when certain numbers are subtracted
import java.io.*;
public class Subtract
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number upto which you want to subtract from the sum");//Print statement
int i=Integer.parseInt(br.readLine());//Taking input from user
int sum = 100;//Initializing sum=0
//Beginning of for loop
for (int j=1; j<=i; j++)
{
sum-= j;//Compound assignment operator being used here
}//end of for loop
System.out.println("Result " +sum);
}// end of main
}// end of class

在示例代码中,我们看到输入了数字 5,从数字 100 中减去直到 5 的总和,我们得到答案为 85。

输出:

Java 中的赋值运算符

3.复合乘法赋值运算符

该程序可用于将数字乘以用户输入的某个数字。我们看到一个程序,用于在 for 循环中打印某个数字与数字的乘法。

代码:

//Program to print the multiplication uptil ith natural number
import java.io.*;
public class Multiply
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number upto which you want to print the multiplication");//Print statement
int i=Integer.parseInt(br.readLine());//Taking input from user
int prod=1;//Initializing prod=1
//Beginning of for loop
for (int j=1; j<=i; j++)
{
prod*= j;//Compound assignment operator being used here
}//end of for loop
System.out.println("Result " +prod);
}// end of main
}// end of class

We enter the number as 5, and then we see that the result is the multiplication of the number with numbers below. In other words, this program shows the factorial of a number in simple terms. We see the output of the program in the below screen.

Output:

Java 中的赋值运算符

4. Compound Division Assignment Operator

In this case, we are going to see the division of a number using the division operator. We won’t be using any kind of loop in this case, but we are going to see the numerator and the denominator. We will input the value of the numerator and divide it by 10 and produce the output to the user.

Code:

//Program to print the division of a number
import java.io.*;
public class Divide
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the numerator");//Print statement
int i=Integer.parseInt(br.readLine());//Taking input from user
i/=10;// Compound Division assignment operator
System.out.println("Result " +i);
}// end of main
}// end of class

In the program, we input 100 as a number, and we divide it by 10 using the Compound Division Assignment operator. We get the output finally as 10, as shown in the below screenshot.

Output:

Java 中的赋值运算符

5. Compound Operators (Remaining Operators)

In the below program, we will see the working of the remaining operators present. The remaining operators are %, ^, &, >>, << and >>>The following is the code and output.

Code:

class CompoundAssignment
{
public static void main(String args[])
{
byte b2 = 127;
b2 %= 7;
byte b3 = 120;
b3 &= 40;
short s1 = 300;
s1 ^= 100;
byte b4 = 127;
b4 >>= 3;
short s2 = 100;
s2 <<= 3;
short s3 = 200;
s3 >>>= 4;
System.out.println("Value of b2= "+b2);
System.out.println("Value of b3= "+b3);
System.out.println("Value of b4= "+b4);
System.out.println("Value of s1= "+s1);
System.out.println("Value of s2= "+s2);
System.out.println("Value of s3= "+s3);
}
}

In the output, we see the result of the compound assignment operations that were left. The output has been printed correspondingly.

Output:

Java 中的赋值运算符

Conclusion – Assignment Operators in Java

This article sees two kinds of Assignment operators- Simple Assignment operators and Compound Assignment operators. We see the working with the help of coding examples. There are advantages as well as disadvantages of using Compound Assignment operators. Assignment operators are used in all other programming languages like C, C++, Python, and wherever value has to be assigned to a variable. The only constraint is that the value has to be of the same data type as the variable which is declared.

以上是Java 中的赋值运算符的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn