As already mentioned, == checks if the same object is, while equals() checks the same content (normal, the base implementation is ==, but String overrides this).
Note:
"Hello" == "Hello" //most probably would be true "Hello".equals( "Hello" ) //will be true String s1, s2; //initialize with something different than a literal, eg loading from a file, both should contain the same string s1 == s2 //most probably will NOT be true s1.equals( s2) //will be true, if both contain the same string, eg "Hello"
In addition, the same is true for wrappers of objects of primitives, for example
Long l1 = 1L; Long l2 = 1L; l1 == l2 //will most likely be true for small numbers, since those literals map to cached instances l1.equals(l2) //will be true new Long(1) == new Long(1) //will NOT be true new Long(1).equals(new Long(1)) //will be true
source share