Assignment operator | Java

Assignment operator | Java

Basics • Java Fundamentals

In Java, the assignment operator (=) is used to assign a value to a variable. It associates the value on the right-hand side with the variable on the left-hand side. Here's a simple example:

int x; // Declare a variable
x = 10; // Assign the value 10 to the variable x

In this example, the variable x is declared and then assigned the value 10 using the assignment operator. After this assignment, the variable x holds the value 10.

You can also combine the declaration and assignment in a single line:

int y = 5; // Declare and assign the value 5 to the variable y

Here, the variable y is both declared and assigned the value 5 in a single line.

The assignment operator can be used with variables of different data types, including primitive types (such as int, double, char, etc.) and reference types (such as objects).

double pi = 3.14; // Declare and assign the value 3.14 to the variable pi
String message = "Hello, World!"; // Declare and assign a string to the variable message

Types of assignment operators

There are three types of assignment operators -

Simple assignment operator

Assigning a single value to a single variable using equal operator is called single assignment operator. For example -

int a;
a = 20;
System.out.println(a); // 20

Chain assignment operator

  • Assigning same value to multiple variables using an operator at a time is called chain assignment operator.
int a, b, c, d;
a = b = c = d = 20;
System.out.println(a + "..." + b "..." + c "..." + d); // 20...20...20...20
  • We can't perform chain or assignment directly at the time of declaration.

  • If you do compiler will get compiled time errors saying cannot find symbol.

int a, b, c;
a = b = c = d = 20;
System.out.println(a + "..." + b "..." + c "..." + d); // 20...20...20...20

Compound assignment operator

  • Assignment operator mixed with some other operator called compound assignment operator.
int a = 10;
a += 20;
System.out.println(a); // 30
  • They are not much but some compound assignment operator that is the only 11 possible compound operator.

    +=, -=,*=,/=,%=,&=,|=,^=,>>=(Right shift operator),<<=(Left shift operator),>>>= (Right shift unsigned operator)

  • In the case of compound assignment operator internal type casting will be performed automatically.

byte b = 10;
b = b + 1; //incompatible types: possible lossy conversion from int to byte
b++;
System.out.println(b); // 11
b += 1;
System.out.println(b); // 12
byte c = 127;
c += 3;
System.out.println(c); // -126
  • We can apply compound assignment operator with chained assignment operator also.
int a, b, c;
a += b -= c *= d /= 2;
System.out.println(a + "..." + b "..." + c "..." + d); // -160...200...200...10

Conclusion

Understanding the assignment operator in Java is fundamental for variable manipulation. Whether using simple, chain, or compound assignments, mastering these concepts is crucial for efficient and error-free programming.

Did you find this article valuable?

Support Xander | ᓕᘏᗢ by becoming a sponsor. Any amount is appreciated!