Equality vs Identity: == for Primitives
What you'll learn: How the == operator compares primitive values to determine if they're equal.
The Core Idea
When you use == with primitive types (int, double, char, boolean, etc.), Java compares the actual values stored in those variables. If the values are identical, == evaluates to true; otherwise, it returns false.
Think of it like comparing two thermometers: if both read 72°F, they show the same temperature—they're equal. The == operator does exactly this for primitive data.
How It Works
int x = 10;
int y = 10;
int z = 20;
boolean result1 = (x == y); // true, both hold value 10
boolean result2 = (x == z); // false, 10 is not equal to 20
The comparison is straightforward: Java looks at what's stored in x and y, sees they're both 10, and returns true. For x and z, the values differ, so == returns false.
Works Across All Primitives
double a = 3.14;
double b = 3.14;
boolean same = (a == b); // true
char letter1 = 'A';
char letter2 = 'B';
boolean match = (letter1 == letter2); // false
Whether you're comparing integers, floating-point numbers, characters, or booleans, == always checks if the values themselves are equal.
Why "Equality vs Identity" Matters
The term "identity" becomes important later when you work with objects (which you haven't learned yet). For now, just know: with primitives, == means "are these values the same?"—plain and simple.
Key Takeaway: The == operator compares primitive values directly—if two primitive variables hold the same value, == returns true.