01 / 21
= ==

Assignment vs.
Equality in Java

Master the difference between =, ==, and .equals(): three operators that look similar but do fundamentally different things.

Java Fundamentals

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.

= Assignment
Changes a variable's value. Used in declarations and updates.
== Comparison
Tests if two primitive values are identical. Returns a boolean.
.equals() Content
Tests if two objects have the same content. Essential for Strings.

The Core Rule

= Assign "Store this value" x = 5; Changes state Returns assigned value == Compare "Are these equal?" x == 5 No state change Returns boolean .equals() "Same content?" s.equals(t) No state change Returns boolean

Primitives: Value in the Box

For primitive types (int, double, boolean, char), the variable directly holds the value. The == operator compares these values directly.

a 42 b 42 == true (same value)

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.

s1 s2 "hello" address: 0x1A "hello" address: 0x2B s1 == s2 → false (different addresses) s1.equals(s2) → true (same content)

== vs .equals()

== for Primitives
Compares the actual values. Works correctly for int, double, char, boolean.
== for Objects
Compares memory addresses. Two objects with identical content will return false.
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

1
Identify the goal. Are you storing a value, or comparing two values?
2
If storing: use = with a variable on the left and the value on the right.
3
If comparing primitives: use == inside an if-statement or boolean expression.
4
If comparing objects: use .equals(). This is the standard for Strings and most other objects.
5
Guard against null: call .equals() on the object you know is not null, or check for null first.

Example 1: Basic Assignment and Comparison

Example
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

Example
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

Spot the 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

✕ Using == for Strings
Writing str1 == str2 when you mean to compare content. This compares references, not characters.
✕ Assignment in if()
Writing if (x = 5) instead of if (x == 5). One equals sign assigns; two compare.
✕ Calling .equals() on null
Writing 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;

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);

Question 3 of 5

Which line correctly checks whether score is equal to 100?

Question 4 of 5

What is the safest way to check if a String variable input equals "yes", even if input might be null?

Question 5 of 5

What does == compare when used on two object variables?