5

...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?

MaVVamaldo
  • 2,505
  • 7
  • 28
  • 50
  • 1
    I don't want to go off topic, but I want to add this information I think it would be useful to the ones who stumbles in this question. My problem was to be able to handle objects implementing content-equality as unique elements. Of course the other side of my problem was to use them inside an HashMap as key elements. There exist a Map implementation to serve this purpose, the [IdentityHashMap](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/IdentityHashMap.html). – MaVVamaldo Feb 27 '12 at 08:00

5 Answers5

5

If you want to check reference just perform:

one == two

But be careful with strings. There is a thing called String constant pool so they may refer to the same object.

tom
  • 2,735
  • 21
  • 35
  • 1
    You're right, but the "constant pool" is a datastructure in class files, the other thing is a *string intern pool*, the technique you refer to is called string interning. – Andreas Dolk Feb 26 '12 at 13:16
3

String in java uses a String Literal Pool, this means is: "When you try construct a string, first String class search in Literal Pool for traditional same string ,if exist return it, and if don't exist create it", so you can't check by equals method compare refernce of String instance, you have to use == operator as following:

String one = new String("yay!");
String two = new String("yay!");
if(one.equals(two))
   System.out.println("equals() returns true.");
if(one == two)
   System.out.println(" == operator returns true.");

result is :

equals() returns true.

see following link for more information:

  1. http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/
  2. Java String.equals versus ==
Community
  • 1
  • 1
Sam
  • 6,770
  • 7
  • 50
  • 91
  • 1
    This answer fails to define reference equality. It then mentions the complication with String, but "When you try construct a string,.." is proven wrong by the sample output; and if it was correct "you have to use == operator" would be incorrect. See the answer by @tom or Wojtek. – SensorSmith Jun 23 '20 at 23:34
2

Use simple == comparison. However to avoid String interning you have to create your Strings using char arrays such as: String me = new String(new char[] { 'm', 'e' }); instead of using String literals "me" such as String me = new String("me");.

Wojciech Owczarczyk
  • 5,595
  • 2
  • 33
  • 55
  • very good thing to know, thank you. Unfortunately I have no control over the String creation since they are instatiated by JAX2B marshaller form an XML input. Maybe it can be configured to avoid the String interning mechanism, but I don't think so. – MaVVamaldo Feb 27 '12 at 09:22
1
if (one == two)
    System.out.println("one and two are the same object");
jbowes
  • 4,062
  • 22
  • 38
1

The only this you need is "==" equality operator.

Artem Barger
  • 40,769
  • 9
  • 59
  • 81