In Java, you might think ==
and .equals()
do the same thing — but they don’t.
Let’s break down the difference in a super simple way π
==
Operator
-
Compares references (memory addresses).
-
Checks if two variables point to the same object in memory.
Example:String a = new String("Java");
String b = new String("Java");
System.out.println(a == b); // false ❌
.equals()
Method-
Compares the actual content (data inside the object).
-
Works for objects like
String
,ArrayList
, etc.
-
No comments:
Post a Comment