Hi, here is the differnce between to operations:
Compares values for equality. Because this method is defined in the Object class, from which all other classes are derived, it's automatically defined for every class. However, it doesn't perform an intelligent comparison for most classes unless the class overrides it. It has been defined in a meaningful way for most Java core classes. If it's not defined for a (user) class, it behaves the same as ==.
Compares references, not values. The use of
== with object references is generally limited to the following:
- – Comparing to see if a reference is null.
- – Comparing two enum values. This works because there is only one object for each enum constant.
- – You want to know if two references are to the same object
String s2 = new String(s1); System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2)); // True, equal Values
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2)); // False, not same reference