Java Operations ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-07-11

The Assignment Operator (=) in Java

The assignment operator = is the most frequently used operator in Java: it stores the value of an expression in a variable. What many beginners miss is that an assignment in Java is not just a statement — it is an expression that produces a value of its own. That single property explains both chained assignments like x = y = z = 100 and popular idioms such as reading a file line by line inside a while condition.

Assignment Operator Syntax

The general form of the assignment operator in Java is:

variable = expression;

The expression on the right is evaluated first, in full; only then is the result stored in the variable on the left. The left side must be something assignable — a variable, an array element, or an object field. Writing 10 = x; is a compilation error.

The variable and the expression must have compatible types. You can assign a byte or short value to an int (the widening conversion happens automatically), but assigning a long to an int without an explicit cast is rejected by the compiler:

int i = 100;
long l = i;      // OK: widening conversion int -> long
// int k = l;    // Compilation error: requires an explicit cast (int) l

Besides the plain =, Java offers a whole family of compound assignment operators+=, -=, *=, /=, %= and others — that combine an arithmetic or bitwise operation with an assignment. They are a separate topic of their own; the logical variants &=, |=, ^= are covered in the lesson on logical operators.

Chained Assignments and Evaluation Order

Because an assignment produces a value, assignments can be chained together — a convenient way to initialize several variables with the same value at once:

int x, y, z;
x = y = z = 100; // all three variables now hold 100

The = operator is right-associative, so the chain is evaluated from right to left: x = (y = (z = 100)). First z receives 100; the value of the expression z = 100 is 100, and it is assigned to y; finally, the value of y = ... is assigned to x.

Important

The chain x = y = z = 100 runs from right to left: first z = 100, then y = z, then x = y. That is why every variable in the chain must already be declared, and each type must be compatible with the value coming from the right.

Note that chaining works only for assignment. You cannot declare several variables in one line as int a = b = 5; unless b is already declared — in a declaration, each variable gets its own initializer.

Assignment as an Expression

The expression variable = value has a value of its own — the value that was just assigned. This means an assignment can appear inside another expression:

int a;
int b = (a = 5) + 10; // a = 5, b = 15

The classic practical use is reading data in a loop, where the assignment and the check of its result share one condition:

String line;
while ((line = reader.readLine()) != null) { // read while lines remain
    System.out.println(line);
}

The parentheses around line = reader.readLine() are mandatory: != has a higher precedence than =, so without them the compiler would try to assign the result of the comparison to line.

This flexibility has a downside that is easy to get wrong: mixing up = and ==. With numbers such code does not compile, but with a boolean it does:

boolean flag = false;
if (flag = true) { // ASSIGNMENT, not comparison! The condition is always true
    System.out.println("Runs every time");
}

Here the comparison operator == was intended, but the assignment operator was written instead: flag receives true, that value becomes the value of the whole expression, and the branch always runs. For boolean variables it is safer to write simply if (flag) or if (!flag) — then the typo cannot happen.

Primitives vs References: What Actually Gets Copied

The = operator always copies the value stored in the variable. For primitive types, that value is the number or boolean itself; for reference types, it is a reference to the object, not the object itself:

Variable type What b = a copies Are the variables linked afterwards
Primitive (int, double, boolean…) The value itself No: changing b does not affect a
Reference type (object, array, String…) A reference to the object Yes: both variables point to the same object

With primitives everything is intuitive — the copy is independent:

int a = 10;
int b = a; // b gets a copy of the value
b = 20;
System.out.println(a); // 10 - variable a is unchanged

With reference types the picture is different: after the assignment both variables point to the same object, and a change made through one variable is visible through the other:

StringBuilder sb1 = new StringBuilder("Java");
StringBuilder sb2 = sb1;   // the reference is copied, the object is shared
sb2.append(" 21");
System.out.println(sb1);   // Java 21 - the change is visible through sb1!

Important

Assigning a reference type never copies the object — only the reference to it. If you need an independent copy of an object or array, create it explicitly: with a copy constructor, clone(), Arrays.copyOf(), and so on. The assignment itself is equally cheap for any type.

Frequently Asked Questions

In what order is the chain x = y = z = 100 evaluated?

From right to left: the assignment operator is right-associative, so the chain is equivalent to x = (y = (z = 100)). First z receives 100, then the value of the expression z = 100 is assigned to y, and finally to x. All three variables end up equal to 100.

What is the difference between = and == in Java?

A single = is an assignment: it stores a value in a variable. A double == is an equality comparison that returns a boolean. The typo if (flag = true) with a boolean variable compiles and makes the condition always true, so for boolean variables write if (flag) with no comparison at all.

Is the object copied when one variable is assigned to another?

No. For reference types the = operator copies only the reference, so both variables point to the same object: a change made through one variable is visible through the other. To get an independent copy, copy the object explicitly — for example, with a copy constructor or Arrays.copyOf() for arrays.

Can an assignment be used inside a condition or another expression?

Yes: an assignment in Java is an expression whose value is the value just assigned. The line-reading idiom relies on it: while ((line = reader.readLine()) != null). The parentheses around the assignment are required because = has a lower precedence than the comparison. Use the trick sparingly — it hurts readability.

Comments

Please log in or register to have a possibility to add comment.