Object Equality
Object equality answers whether two values should count as the same in your domain. The equals method expresses that rule, while == compares references.
Equal objects must produce equal hash codes. Hash-based sets and maps depend on that contract to find values reliably.
Compare values by content
public class Main {
public static void main(String[] args) {
String first = new String("java");
String second = new String("java");
System.out.println(first == second);
System.out.println(first.equals(second));
}
}
Output:
false
true
The two strings contain the same characters but are separate objects. Reference comparison with == is false, while equals compares their content and returns true.
For an ordinary class, override equals and hashCode together. Mutable fields make poor equality components when an object is used as a map key.
Use a Badge record so equal component values produce equal objects and hash codes.