Java's Basic and Shorthand Assignment Operators

Basic Assignment Operator

Java's basic assignment operator is a single equals-to (=) sign that assigns the right-hand side value to the left-hand side operand. On left side of the assignment operator there must be a variable that can hold the value assigned to it. Assignment operator operates on both primitive and reference types. It has the following syntax:

var = expression;
Note that the type of var must be compatible with the type of expression.

Chaining of assignment operator is also possible where we can create a chain of assignments in order to assign a single value to multiple variables. For example,

int x, y, z;
x = y = z = 100; // set x, y, and z to 100

The above piece of code sets the variables x, y, and z to 100 using a single statement. This works because the = is an operator that yields the value of the right-hand expression. Thus, the value of z = 100 is 100, which is then assigned to y, which in turn is assigned to x. Using a "chain of assignment" is an easy way to set a group of variables to a common value.

Reference Assignment

A variable referring to an object is a reference variable not an object. We can assign a newly created object to an object reference variable as follows:

Student st1 = new Student();

The above line of code performs three tasks

  • Creates a reference variable named st1, of type Student.
  • Creates a new Student object on the heap.
  • Assigns the newly created Student object to the reference variable st1

We can also assign null to an object reference variable, which simply means the variable is not referring to any object:

Student st2 = null;

Above line of code creates space for the Student reference variable st2 but doesn't create an actual Student object.

Note that, we can also use a reference variable to refer to any object that is a subclass of the declared reference variable type.

Primitive Assignment

Primitive variables can be assigned either by using a literal or the result of an expression. For example,

int x = 130; // literal assignment
int y = x + 2; // assignment with an expression including a literal
int z = x * y; // assignment with an expression

The most important point that we should keep in mind, while assigning primitive types is if we are going to assign a bigger value to a smaller type, we have to typecast it properly. In Above piece of code the literal integer 130 is implicitly an int but it is acceptable here because the variable x is of type int. It gets weird if you try 130 to assign to a byte variable. For example, The below piece of code will not compile because literal 130 is implicitly an integer and it does not fit into a smaller type byte x. Likewise, byte c = a + b will also not compile because the result of an expression involving anything int-sized or smaller is always an int.

byte x = 130; // error: possible loss of precision
byte a = 2;
byte b = 3;
byte c = a + b; // error: possible loss of precision

Primitive Casting

Casting lets us convert primitive values from one type to another (specially from bigger to smaller types). Casts can be implicit or explicit. An implicit cast means we don't have to write code for the cast; the conversion happens automatically. Typically, an implicit cast happens when we do a widening conversion. In other words, putting a smaller thing (say, a byte) into a bigger container (like an int). Remember those "possible loss of precision" compiler errors we saw during assignments. Those happened when we tried to put a larger thing (say, an int) into a smaller container (like a byte). The large-value-into-small-container conversion is referred to as narrowing and requires an explicit cast, where we tell the compiler that we are aware of the danger and accept full responsibility.

As a final note on casting, it is very important to note that the shorthand assignment operators let us perform addition, subtraction, multiplication or division without putting in an explicit cast. In fact, +=, -=, *=, and /= will all put in an implicit cast. Below is an example:

byte b = 3;
b += 7; // No problem - adds 7 to b (result is 10)
// and is equivalent to
byte c = 3;
c = (byte) (c + 7); // Won't compile without the cast, since c + 7 results in an int

Shorthand Assignment Operators

In addition to the basic assignment operator, Java also defines 12 shorthand assignment operators that combine assignment with the 5 arithmetic operators (+=, -=, *=, /=, %=) and the 6 bitwise and shift operators (&=, |=, ^=, <<=, >>=, >>>=). For example, the += operator reads the value of the left variable, adds the value of the right operand to it, stores the sum back into the left variable as a side effect, and returns the sum as the value of the expression. Thus, the expression x += 2 is almost the same x = x + 2.

The difference between these two expressions is that when we use the += operator, the left operand is evaluated only once. This makes a difference when that operand has a side effect. Consider the following two expressions a[i++] += 2; and a[i++] = a[i++] + 2;, which are not equivalent:

public class ShorthandOprDemo1
  public static void main(String []args)
    int a[] = new int[5];
    int i = 0;
    a[i++] += 2; // i is incremented only once
    a[i++] = a[i++] + 2; // i is incremented twice

Last Word

In this tutorial we discussed basic and shorthand (compound) assignment operators of Java. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!


Get Free Tutorials by Email

About the Author

is the main author for He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

Facebook to exclude billions from European privacy lawsPosted on Thursday April 19, 2018

A total of 1.5 billion international Facebook users will not be protected under GDPR.

TalkTalk and Vodafone top complaints chart againPosted on Thursday April 19, 2018

TalkTalk, Vodafone and BT drew the most complaints for their services, the regulator says.

Ikea's TaskRabbit app back online after data breachPosted on Thursday April 19, 2018

The odd jobs marketplace has been investigating a "cyber-security incident".

Courtesy BBC News


Advertisements help running this site for free.

To view the content please disable AdBlocker and refresh the page.