This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Using == for String Equality? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using == for String Equality?" Watch "Using == for String Equality?" New topic
Author

Using == for String Equality?

Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

It was my understanding that == should not be used for testing String equality. However, the code below works;



Why does that code work and output "Equal"?

Are there any circumstances where it is OK to use == to test for String equality?

Thanks!


Beginner
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

It only seems to work.

The == operator on reference types always does exactly the same thing: compares whether the two reference values are the same. That is, whether both references point to the same object, or both are null. It never compares objects' states (contents).

In this case, you seem to be assuming that you have two separate String objects. You don't. String literals are compiled into the .class file and added to a JVM-wide constant pool when the containing class is loaded, if an equal String isn't already there. So the two String references expressions, "aa" and "aa", both point to the same String object.

Are there any circumstances where it is OK to use == to test for String equality?


No. You should never rely on this behavior to shortcut to == when your semantics are about Strings' contents.
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Are there any circumstances where it is OK to use == to test for String equality?

Never :-)

The jvm saves "aa" in the string pool then your equality check == refers to the same string in both sides and succeed.

PD: Sorry Jeff Verdegan responded while I was typing :-)


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Your question has been discussed many times before. Have a look, for example, at the following FAQ page and topics:

http://www.coderanch.com/how-to/java/AvoidTheEqualityOperator

a.equals(b)
Comparing objects using ==operator.
What is difference ?
String constant pool
Why strings are not equal?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7487
    
  18

Fergal Crawley wrote:It was my understanding that == should not be used for testing String equality.

Correct; and furthermore, with very few exceptions, it should not be used to test any object's equality. Have a look at the AvoidTheEqualityOperator (←click) page for details.

And if you're interested in the "caching" Jeff referred to, you might want to check out this page.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

Thanks for your replies. Given your explanations, I didn't expect the code below to work and output "Equal", but it does. Why is that?

Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Hi,

Take a look at the link provided by Winston Gutkowski there is well explained.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Fergal Crawley wrote:Thanks for your replies. Given your explanations, I didn't expect the code below to work and output "Equal", but it does. Why is that?


This is the same question you initially asked. I explained exactly why it does that. Others also explained it, and provided links to where you can read more about it.

So what do you still not understand? What about those explanations and links leads you to believe that == will be false here?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Note that the == operator only returns true if the operands on the left and right side refer to the exact same object.

The Java compiler performs an optimization with string literals. If the same string literal, for example "aa", is used multiple times in the same program, then the compiler makes sure that only one String object is created, which is reused.

Because of this, your variables a and b refer to the same string object, and so you get true when you compare them with ==.

See: https://www.coderanch.com/how-to/java/CachedObjects
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

Try this:


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Fergal Crawley
Greenhorn

Joined: Nov 28, 2011
Posts: 18

Thanks for your help Jeff. I felt it was a different question because I used variables second time round. I wasn't aware that;

Jesper de Jong wrote:...The Java compiler performs an optimization with string literals. If the same string literal, for example "aa", is used multiple times in the same program, then the compiler makes sure that only one String object is created, which is reused...


Thanks for that Jesper. Also I hadn't seen some of the earlier replies (with the links) as they were written when I was replying with my second question.

Thanks again everyone, I understand it now.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Fergal Crawley wrote:Thanks for your help Jeff. I felt it was a different question because I used variables second time round.


I see. I didn't even notice that as I was scrolling back and forth between the two code samples. It doesn't matter if you use member variables, local variables, the literal expressions directly, the result of method calls, or a combination of any two of those. All are expressions of type "reference to String," and == will be true for any two of them if they both refer to the same String object.



In the above, "aa", s1, s2, and getStr() are all expressions that evaluate to a reference to a String object, and because of the constant pool, they all happen to point to the same String object.

I wasn't aware that;

Jesper de Jong wrote:...The Java compiler performs an optimization with string literals. If the same string literal, for example "aa", is used multiple times in the same program, then the compiler makes sure that only one String object is created, which is reused...



Just FYI, that's basically the same thing I said in my first reply. Not trying to steal credit from Jesper since he's the one that wrote it in a way that made sense to you. :-) Just wanted to point it out, in case re-reading with the foreknowledge that it says basically the same thing helps the pieces fit into place a little better.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
It goes to show you: two people can say the same thing and one of them says it in the manner you actually understand. That happens all the time.
It may be worthwhile learning about some of the strange behaviour of Strings in an old JavaRanch Journal.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using == for String Equality?
 
Similar Threads
Doubts in Strings
Object confusion
static methods and NULL
Operators and Precedence
String == (Abhilash mock)