r/javahelp • • Sep 12 '25

Codeless == compares all object attributes so why everyone says it’s wrong?

Why everybody talks nonsense when talking about == operator in Java? It’s simple as comparing all objects’ attributes otherwise it wouldn’t make sense and the developers wouldn’t have done it

0 Upvotes

34 comments sorted by

View all comments

Show parent comment

2

u/c_dubs063 Sep 12 '25

If you are comparing primitives, like two int's, the comparison is between the values assigned to those primitives.

int var1 = 5;
int var2 = 6;
boolean isEqual = 5 == 6; // evaluates to false because 5≠6

If you are comparing two objects, the comparison is between the memory addresses where those objects are stored in memory.

Object obj1 = new Object(); // stored at 0x0001
Object obj2 = new Object(); // stored at 0x0002
boolean isEqual = obj1 == obj2; // evaluates to false because 0x0001≠0x0002