3

Possible Duplicate:
why equals() method when we have == operator?

When I tried executing the code in Java, it gave me 2 different outputs

String txt1="Hello";
String txt2="Hello";
System.out.println((boolean)txt1==txt2);

String txt1=new String("Hello");
String txt2=new String("Hello");
System.out.println((boolean)txt1==txt2);
Community
  • 1
  • 1
user1247347
  • 201
  • 2
  • 4
  • 5
  • http://stackoverflow.com/questions/390703/what-is-the-purpose-of-the-expression-new-string-in-java this will be helpful. – RP- Nov 05 '12 at 13:04
  • 1
    also no need to cast result of `==` operator to `boolean` – jlordo Nov 05 '12 at 13:08

5 Answers5

8

Strings are objects. == compares object references, not the content of the strings. To do that, use the String#equals method.

In your first example, txt1 and txt2 are two variables pointing to the same String object. So they're == to each other.

In your second example, txt1 and txt2 are pointing to two different String objects (which have the same sequence of characters), and so they aren't == to each other.


Separately: There's almost never any point to writing new String("string literal"). If you don't know specifically a really, really good reason to do it, don't. There are only a couple of very, very, very unusual situations where you might do that, which relate to interacting with low-level stuff. Not in normal, portable Java code.

There is occasionally a reason to use new String(String) (not a string literal, but an instance you got from somewhere else, like substring). See this article for more on that (thanks Rp-).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • then why would the first one return true? – user1247347 Nov 05 '12 at 13:05
  • @user1247347: Because `txt1` and `txt2` are variables pointing to the *same* object. (Click refresh to make sure you're reading my complete answer, I realized that my first quick answer might not have been sufficient and added to it.) – T.J. Crowder Nov 05 '12 at 13:07
  • http://kjetilod.blogspot.in/2008/09/string-constructor-considered-useless.html this blog explains the rare usages of `new String(someStr);`.. Follow the comments as well. – RP- Nov 05 '12 at 13:13
  • @MarkoTopolnik: Indeed, I've clarified it. – T.J. Crowder Nov 05 '12 at 13:16
3

The == operator will check for reference equality, that is, will return true if the two argument Strings are the same instance.

Whenever a String literal (for instance "Hello") occurs in a class, a String instance is interned (kind of stored in an internal cache so it can be reused).

After doing String txt1="Hello", txt1 will be very same reference of the interned String. So,

String txt1="Hello";
String txt2="Hello";

Results in txt1 and txt2 being the same instance, that is, the interned one.

When you're doing String txt1=new String("Hello"), it's calling the String constructor with the interned instance as an argument (kind of a copy constructor). So, txt1 will be a new String instance holding the same value as the interned instance, and the == operator will return false.

More information on the subject can be found in the 3.10.5. String Literals section of the JLS.

A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

The following question's answer explain When are Java Strings interned?. The following link elaborates on the subject: String Equality and Interning.

As a side note, remember to use equals() in order to perform String comparisons based on their contents.

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • When I tried debugging them, txt1 and txt2 in the second case had different IDs. So what are these IDs? Are they some kind of value given to the object based on the memory location? – user1247347 Nov 05 '12 at 13:09
  • That's right, it's the value that identifies the instance. They're effectively differents `String` instances holding the same value, so `==` returns `false`. – Xavi López Nov 05 '12 at 13:10
2

Java, where possible, tries to "share" strings to safe space.

String txt1="Hello";
String txt2="Hello";

are two references to the same Object ("Hello")

String txt1=new String("Hello");
String txt2=new String("Hello");

Are two references to two different instances, each initialised by copy.

If you compare Strings, always use "equals()" because otherwise results are hard to predict.

thedayofcondor
  • 3,860
  • 1
  • 19
  • 28
0

If you want to compare references then use == operator.

If you want to compare contents of two strings use equals method.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

Do this

String txt1="Hello";
String txt2="Hello";
System.out.println(txt1.hashCode());
System.out.println(txt2.hashCode());
System.out.println((boolean)txt1==txt2);


String txt1=new String("Hello");
String txt2=new String("Hello");
System.out.println(txt1.hashCode());
System.out.println(txt2.hashCode());
System.out.println((boolean)txt1==txt2);

You can get how java handles internally.

vels4j
  • 11,208
  • 5
  • 38
  • 63