The Java String object extends the Java Object like any other Java object does. It must however be noted that the Java String overrides Object.equals method to compare two String objects character by character.
Let's briefly look at the Object.equals code:
public boolean equals(Object other)
{
return this == other;
}
{
return this == other;
}
We can see that Object.equals compares the references of the objects themselves and as such returns true only if the two references are in essence referring to the same object in heap. The equals is overriden by Java String to return true if the two objects are logically equal.
Consider,
String a = "abc";
String b = new String ("abc");
String c = "abc";
System.out.println (a.equals(b));
System.out.println (a.equals(c));
System.out.println (a == b);
System.out.println (a == c);
The output would be:
true
true
false
true
No comments:
Post a Comment