ホームページ  >  記事  >  Java  >  Java の代入演算子

Java の代入演算子

WBOY
WBOYオリジナル
2024-08-30 15:19:57864ブラウズ

Java の代入演算子は、単純な代入演算子と複合代入演算子など 2 つのカテゴリに分類されます。名前が示すように、代入演算子は、演算に関与する変数に値を割り当てるために使用されます。単純な代入演算子は、加算、減算、乗算、除算などの単純で単純な演算を処理します。複合代入演算子は、^、&、%、<>、>>、<< など、コード内でさらに論理演算が必要な場合に使用されます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

代入演算子には通常 2 つのタイプがあります。それらは次のとおりです:

  • 単純な代入演算子
  • 複合代入演算子

単純代入演算子は「=」記号とともに使用され、左側がオペランドで構成され、右側が値で構成されます。右側の値は、左側で定義されているのと同じデータ型である必要があります。

+、-、*、/ が = 演算子とともに使用される場合、複合演算子が使用されます。

代入演算子の種類

さまざまなタスク演算子があります。割り当ての結果、割り当て後の目的/ターゲット変数の値が得られます。

単純な代入演算子

まず、Java プログラムを使用して Simple Assignment 演算子の動作を確認してみます。プログラムには、2 つの値を数値 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 ステートメントを実行すると、出力として次のものが得られます。先ほど初期化した 2 つの数値が出力されたことがわかります。どちらの数値も値 5 で初期化されました。

出力:

Java の代入演算子

複合代入演算子

ここでは、複合代入演算子の動作を確認します。以下は複合代入演算子のリストです。

  • += 複合追加代入演算子
  • -= 複合減算代入演算子
  • *= 複合乗算代入演算子
  • /= 複合除算代入演算子。

上記は、存在する 4 つの基本的な複合代入演算子です。他にも次のような複合代入演算子が存在します。

  • %= 複合モジュロ代入演算子。
  • &= 複合ビット単位代入演算子。
  • ^= 複合ビット単位 ^ 代入演算子。
  • >>= 複合右シフト代入演算子。

  • >>>= 複合右シフト埋め込み 0 代入演算子。
  • <<= 複合左シフト代入演算子。

この記事では、最初の 4 つの複合代入演算子を他の演算子とともに詳細に確認します。複合代入演算子の基本的な利点は、Java 言語プログラム内の多くのコードを節約できることです。

1.複合追加代入演算子

この演算子は、ループ全体を通じて連続的に変数に数値を加算するために使用されます。ループを使用して最初の i 番目の自然数の合計を求めるプログラムを見ていきます。 for ループでは、Compound 追加演算子を使用します。

コード:

//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 と入力すると、45 のような最初の 10 個の自然数の合計が得られることがわかります。

出力:

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 a ループ内で特定の数値と数値の乗算を出力するプログラムが表示されます。

コード:

//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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。