For objects, == checks reference equality, meaning both variables point to the same object. equals() checks logical equality, meaning two objects hold the same content, as long as the class overrides it.
For primitives, == compares the values directly. For objects, the default Object.equals() behaves exactly like ==. Classes such as String and Integer override it to compare their values, and your own classes should too when two instances can be logically equal.
Identical string literals are interned, so they share one object in the string pool and s3 == s4 is true. A string built with new String is always a separate object, so compare strings with equals(). A class that overrides equals() must also override hashCode().
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = "hello";
String s4 = "hello";
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)
System.out.println(s3 == s4); // true (string pool)
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓