This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings "equality"" Watch "Strings "equality"" New topic
Author

Strings "equality"

Mike Petrov
Greenhorn

Joined: Oct 10, 2011
Posts: 21

Hello everyone, I'm a Java newbie, and this is my first post here.
I'm confused about Strings. I know that == checks whether two references lead to the same object, and .equals() checks for the String content itself. Therefore I don't understand why the following code produces true in the output. I've created two Strings, thus I have two different references to the different objects. The only possible clue for me is that somehow these two strings represent one object.
Thanks a lot for your help.

Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
Mike,

Can you check 'string pooling' concept in java. So you can understand better.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Welcome to the Ranch.

As you probably already know, you must always compare strings in Java using equals() and not using ==, because == indeed checks reference equality instead of comparing the content of the objects.

Java optimizes string literals. When you use the same string literal such as "ordinary string" multiple times in your code, then the Java compiler is smart enough to create only one String object and reuse that in all places where you're using the literal. This optimization is possible because class String is immutable (you can't change the content of a String object after it's created).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
make String numberTwo = new String("ordinary string"); now == produces false.

Create another String numberThree = new String("ordinary string");

numberTwo==numberThree results in false.

Never use the "new" keyword when hardcoding the string contents. you are unnecessarily creating too many objects on the heap and wasting memory.


Understanding the concept of String Pool in java lets you know the reason



Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

What the compiler (sort of) does with this code:



is to refactor it into something like this:




When writing it this way, it becomes quite obvious what is going on. And IMHO, this is the way you should use it. Imagine that you handle the ORDINARY_STRING "ordinary string" in 1023 places and your boss tells you to start using the ordinary string "ordinaryString"... Good luck actually finding and changing all occurrences.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Please search JavaRanch for “Strings, literally” and you will find a useful JavaRanch Journal article about that very problem. Actually, try here before searching.
Mike Petrov
Greenhorn

Joined: Oct 10, 2011
Posts: 21

Thank you everyone, think I've understood the point. Of course, I'll check your suggestions. I'll post here if I have any difficulties on this topic.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
You’re welcome
Mike Petrov
Greenhorn

Joined: Oct 10, 2011
Posts: 21

I've looked at the String Pooling concept, it was quite easy, but I got one more question.

If the string is created somehow like this

it is referenced both from the String Pool and from the local variable myString.

But if the string is created using the keyword "new"

it is understood that it is referenced from myString2 but what about referencing from the Pool? Please check my question about this ambiguity in the article.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
If you use the new operator you are telling the JVM you don’t want the String in the pool. It is rarely useful to use new String("..."), except when you have many Strings you want substrings from. Look for earlier discussions, for example, this thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Another possibly useful thread. Did the "strings, literally" link help you?
Mike Petrov
Greenhorn

Joined: Oct 10, 2011
Posts: 21

Yes, thanks a lot. The article helped me, and the second thread too (the first one seemed a bit confusing). It seems that this topic itself is a special case. Even the neighbour post author says that
I suspect many programmers can go their whole careers without having a real need for this.

Therefore it's just useful to know about the Pool and don't forget not to use the "new" except creating substrings. All the rest seems to be from the "20" part of the "80/20" approach for me as a newbie now. Thanks for your help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
More like the 0.2 of the 80/20!

and you’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings "equality"
 
Similar Threads
can any one explain me call by value with one sample program
Parsing the String
Adding elements of two arrays
Testing a wsdl file
While and Do-While Question