首頁 >Java >java教程 >Java 中的賦值運算符

Java 中的賦值運算符

WBOY
WBOY原創
2024-08-30 15:19:57918瀏覽

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