When you use == with a primitive (int, double, char, ...), you are checking whether the values are identical.When you use == with an object, you are checking whether the 2 objects are stored at the same address, in other wordswhether the two references are both pointing to the same object.
.equals() changes. Unless it is overridden by the object class, it is the same as ==. Some classes (like String) haveoverridden this method so that the class to create objects, they can check whether the contents are equivalent.it willcheck the
contents of the object at the two addresses instead of just the addresses themselves.
public class EQUALSeqeq {
public static void main(String args[]) {
String s1=new String("5"); Long l1=new Long("5"); System.out.println(s1.equals(l1)); //displays false
}
}
The equlas( ) method is overridden in String class. But it compares only two String objects.If you are comparing a String object with any other object type, evne though they have same values, false will be returned.
The values in those classes are different. One is a String the other is a wrapper( long ), which is an integral type. How can 2 different types ever be equal.