== so sánh reference — hai biến có cùng trỏ về một object không. .equals() so sánh nội dung bên trong.
Hình dung: == hỏi "có phải cùng một tờ giấy?", còn .equals() hỏi "hai tờ viết giống nhau không?".
java
String a = new String("hi");
String b = new String("hi");
a == b; // false — hai object khác nhau
a.equals(b); // true — nội dung giống nhauVới String hay object bất kỳ luôn dùng .equals(). == chỉ dùng cho primitive, check null, hoặc so sánh enum.
== compares reference — whether two variables point to the same object. .equals() compares content inside.
Picture it: == asks "the same sheet of paper?", while .equals() asks "two sheets with the same writing?".
java
String a = new String("hi");
String b = new String("hi");
a == b; // false — two different objects
a.equals(b); // true — same contentFor Strings or any object, always use .equals().
Use == only for primitives, null checks, or enum comparison.