…in other words:
let’s suppose I have 2 Strings declared as so:
String one = new String("yay!");
String two = new String("yay!");
these two Strings are two different objects, but if I run
if(one.equals(two))
System.out.println("equals() returns true.");
I get “equals() returns true”.
This is because the String class overrides the equals() method to implement a content level equality.
However, I need to access a reference level equality (like the one implemented in Object) to distinguish the object one form the object two.
How can I do that?
I tried this:
one.getClass().getSuperclass().equals();
to try to invoke the Object equals() method of the String one but it didn’t work.
Any advice?
Stringin java uses aString Literal Pool, this means is: “When you try construct a string, first String class search inLiteral Poolfor traditional same string ,if exist return it, and if don’t exist create it”, so you can’t check byequalsmethod compare refernce of String instance, you have to use==operator as following:result is :
see following link for more information: