In Java, there is a difference between java == and equals() method to compare primitives (int, float etc), strings and objects. This is a short article just to clear the difference between the both.
You can tell the difference by just two lines:
== tests for reference equality.
.equals() tests for value equality.
Repeat with me,
== tests for reference equality.
.equals() tests for value equality.
Once more, with feeling
== tests for reference equality.
.equals() tests for value equality.
Here, remember that == first tests the datatype and then checks for reference equality.
Now, that we know the difference between the both, let’s try some code example because if you don’t code, no matter how many books you read or how many articles you go through, you will not know the difference.
So, come on, open up your java code editor and let’s try the examples given below:
String s = "ab"; String s2 = "ab"; String s3 = new String("ab"); String s4 = new String("ab"); String s5 = new String("ab").intern(); String s6 = "test"; String s7 = "te" + "st"; String s8 = "!test";
// true because s and s2 refer to same memory location System.out.println("s == s2 is " + (s == s2));
// false because s3 and s4 are two different objects referring to different memory locations System.out.println("s3 == s4 is " + (s3 == s4));
// false because s4 and s5 are two different objects referring to different memory locations // s4 is a new object, s5 interns an already existing string literal memory location System.out.println("s4 == s5 is " + (s4 == s5));
// true because s is a string literal already existing in the string pool // and s5 after being interned points to the same string System.out.println("s == s5 is " + (s == s5));
// false because s and s3 are two different objects referring to different memory locations // s is a literal in the string pool, s3 is an object System.out.println("s == s3 is " + (s == s3));
// true because concatenation occurs at compile time // and both string literals point to the same memory location in the string pool System.out.println("s6 == s7 is " + (s6 == s7));
// true because both values are same System.out.println("s.equals(s2) is " + s.equals(s2));
// true because both values are same System.out.println("s3.equals(s4) is " + s3.equals(s4));
// true because both values are same System.out.println("s.equals(s3) is " + s.equals(s3));
// true because substring happens at runtime System.out.println("s6.equals(s8.substring(1)) is " + s6.equals(s8.substring(1)));
// true because substring happens at runtime // and s6 which is the literal "test" already exists in the memory System.out.println("s6.equals(s8.substring(1).intern()) is " + (s6.equals(s8.substring(1).intern())));
// true because substring happens at runtime // and s6 which is the literal "test" already exists in the memory // after substring and intern, s8 points to the same memory location System.out.println("s6 == s8.substring(1).intern() is " + (s6 == s8.substring(1).intern()));
According to Oracle String#intern
A pool of strings (String pool), initially empty, is maintained privately by the class
String
.When the intern method is invoked, if the pool already contains a string equal to this
String
object as determined by theequals(Object)
method, then the string from the pool is returned. Otherwise, thisString
object is added to the pool and a reference to thisString
object is returned.
In short, String#intern() is a function which checks for an already existing string in the string pool. If found, returns the reference, otherwise creates a new object and returns the reference.