Assignment Operator in Java | Example Program

An operator which is used to store a value into a particular variable is called assignment operator in java.

In any programming language, an assignment operator is the most commonly used to assign a value in a variable.

There are three categories of assignment operations in java programming. They are as follows:

  1. Simple assignment
  2. Compound assignment
  3. Assignment as expression

Simple Assignment


A simple assignment can be used in two ways:

  • To store or assign a value into a variable.
  • To store a value of a variable into another variable.

It has the following general format to represent a simple assignment.

v = expression;

where,

  • v: It is a variable name that represents a memory location where a value may be stored.
  • expression: It may be a constant, variable, or a combination of constants, variables, and operators.
  • = is an assignment operator.

For example:

1. int x = 10;
2. int y = x; // Here, the value of variable x is stored into y.

Compound Assignment


The general form of compound assignment is as follows:

v op = expression;

where,

  • op: It is a Java binary operator. It may be +  – * / % << >> etc. v and expression are the same as explained in the simple assignment.
  • The operator op = is known as a shorthand assignment operator in Java because
  • v op = expression is shorthand notation for v = v operator expression.

For example:

1. x += 5; // It is equivalent to int x = x + 5;
2. x -= 10; // It is equivalent to int x = x – 10;
3. a *= 100; // Equivalent to int a = a * 100;
4. a /= (b + c);

Let’s take an example program based on compound assignment operator.

Program source code 1:

package assignmentOperatorPrograms; 
public class CompoundTest 
{ 
 public static void main(String[] args) 
 { 
   int x = 20, y = 30, z = 50; 
    x += y; 
    y -= x + z; 
    z *= x * y;
 System.out.println("x = " +x ); 
 System.out.println("y = " +y ); 
 System.out.println("z = " +z ); 
 } 
}
Output: 
       x = 50 
       y = -70 
       z = -175000

Explanation:

1. x += y; will be evaluated in the following steps:
x += y; is equivalent to x = x + y;
x = 20 + 30;
x = 50;


2. y -= x + z; is equivalent to y = y – (x + z);
y = y – ( 50 + 50);
y = 30 – 100;
y = -70;

3. z *= x * y;  is equivalent to z = z * (x * y);
z = z * (50 * (-70));
z = 50 * (-3500);
z = -175000;

Assignment as Expression


In Java, an assignment operation is also considered an expression because the operation has a result. The result of expression is a value that is stored in a variable. It is mainly used in more than one assignment.

For example:

1. int x = y – z + 4; // Here, the expression y – z + 4 is evaluated first and then its result is stored into the variable x.

Let’s take an example program related to this concept.

Program source code 3:

package assignmentOperatorPrograms; 
public class Expression 
{ 
public static void main(String[] args) 
{ 
 int a = 19, b = 31, c = 50; 
  a += 1; 
  b -= 1; 
  c *= 2; 
 int x = (10 + a); 
 int y = x + 100; 
 int z = x + y + c; 
System.out.println("Value of a: " +a); 
System.out.println("Value of b: " +b); 
System.out.println("Value of c: " +c); 
System.out.println("Value of x: " +x); 
System.out.println("Value of y: " +y); 
System.out.println("Value of z: " +z); 
 } 
}
Output: 
       Value of a: 20 
       Value of b: 30 
       Value of c: 100 
       Value of x: 30 
       Value of y: 130 
       Value of z: 260

Explanation: 

The following steps have been performed to evaluate the above expressions of the program.
1. a += 1;
a = a + 1;
a = 19 + 1;
a = 20;

2. b -= 1;
b = b – 1;
b = 31 – 1;
b = 30;

3. c *= 2;
c  = c * 2;
c  = 50 * 2;
c  = 100;

4.  x = (10 + a);
x = 10 + 20; // Here, the value of x will be 20, not 19.
x = 30;

5. y = x + 100;
y = 30 + 100; //  Here, value of x will be 30.
y = 130;

6. z = x + y + c;
z = 30 + 130 +100; // Here, value of c will be 100.
z = 260;

Note:

1. You cannot use more than one variable on the left-hand side of = operator.

For example:

x + y = 20; // It is invalid because there will be doubt to Java compiler regarding for storing the value 20.

2. You cannot use a literal or constant value on the left-hand side of = operator.

For example:

20 = a; // It is also invalid because how can we store value of x in a number.


Hope that this tutorial has covered all important points related to assignment operator in Java with example programs. I hope that you will have understood this topic clearly and enjoyed it.
Thanks for reading!!!

Next ⇒ Unary Operator in Java⇐ PrevNext ⇒

Leave a Comment

x