Assignment vs.
Equality in Java
Master the difference between =, ==, and .equals(): three operators that look similar but do fundamentally different things.
What is Assignment?
The single equals sign = is the assignment operator. It takes the value on the right side and stores it into the variable on the left side. It does not ask a question: it gives an order.
int score = 95; // store 95 into score String name = "Alice"; // store "Alice" into name score = score + 5; // update score to 100
What is Equality Testing?
The double equals sign == is the equality operator. It asks a question: "Are these two values the same?" and returns true or false.
int a = 10; int b = 10; boolean same = (a == b); // true: both hold 10 boolean diff = (a == 20); // false: 10 is not 20
Why Does This Matter?
Confusing = with == is one of the most common bugs in programming. The compiler may not always catch it, and the resulting behavior is silently wrong.
The Core Rule
Primitives: Value in the Box
For primitive types (int, double, boolean, char), the variable directly holds the value. The == operator compares these values directly.
Objects: Reference in the Box
For objects (String, arrays, custom classes), the variable holds a reference (memory address), not the object itself. Two variables can point to different objects with the same content.
== vs .equals()
String a = new String("cat"); String b = new String("cat"); a == b // false: different objects a.equals(b) // true: same content "cat"
The String Pool Trap
Java optimizes string literals by storing them in a String Pool. Identical literals share the same reference, making == appear to work. But this is an optimization detail, not a guarantee.
String x = "hello"; // from pool String y = "hello"; // same pool ref x == y // true (by accident!) String z = new String("hello"); // new object x == z // false! x.equals(z) // true (always reliable)
How to Apply the Rules
= with a variable on the left and the value on the right.== inside an if-statement or boolean expression..equals(). This is the standard for Strings and most other objects..equals() on the object you know is not null, or check for null first.Example 1: Basic Assignment and Comparison
int age = 16; int votingAge = 18; if (age == votingAge) { System.out.println("You can vote!"); } else { System.out.println("Not yet."); } // Output: "Not yet."
Example 2: String Comparison
String input = new String("yes"); String target = "yes"; // Wrong approach: if (input == target) { ... } // false! // Correct approach: if (input.equals(target)) { ... } // true!
Example 3: The Accidental Assignment Bug
int x = 10; // Programmer meant to compare: if (x = 20) { // BUG: assigns 20 to x! System.out.println("Match"); } // Correct version: if (x == 20) { // compares x to 20 System.out.println("Match"); }
Null Safety with .equals()
String userInput = null; // Dangerous: NullPointerException! userInput.equals("hello"); // Safe: call .equals() on the known non-null value "hello".equals(userInput); // returns false, no crash // Alternative: null check first if (userInput != null && userInput.equals("hello")) { // safe }
Common Mistakes
str1 == str2 when you mean to compare content. This compares references, not characters.if (x = 5) instead of if (x == 5). One equals sign assigns; two compare.null.equals(s) crashes with a NullPointerException. Always null-check first.Summary Table
| = | == | .equals() | |
|---|---|---|---|
| Purpose | Assign a value | Compare values | Compare content |
| Returns | The assigned value | boolean | boolean |
| Use for Primitives | ✓ storing | ✓ comparing | ✕ N/A |
| Use for Objects | ✓ storing refs | ✕ compares refs | ✓ compares content |
| Modifies state? | Yes | No | No |
Question 1 of 5
What does the following code do?int x = 42;
= is the assignment operator. It stores the value 42 into the variable x. It does not compare anything.Question 2 of 5
What is the output of this code?
String a = new String("hi");
String b = new String("hi");
System.out.println(a == b);
new String() creates two separate objects in memory. The == operator compares their references (memory addresses), which are different. Use .equals() to compare content.Question 3 of 5
Which line correctly checks whether score is equal to 100?
score is a primitive int, use == to compare. Option A assigns instead of comparing. Option B fails because primitives don't have methods. Option D uses JavaScript syntax, not Java.Question 4 of 5
What is the safest way to check if a String variable input equals "yes", even if input might be null?
"yes".equals(input) is null-safe because the literal "yes" is never null. If input is null, it simply returns false. Option B would throw a NullPointerException.Question 5 of 5
What does == compare when used on two object variables?
== compares memory references, not content. Two objects can contain identical data but live at different memory addresses, so == returns false. Use .equals() for content comparison.