• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings "equality"

 
Greenhorn
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,

Can you check 'string pooling' concept in java. So you can understand better.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Mike Petrov
Greenhorn
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another possibly useful thread. Did the "strings, literally" link help you?
 
Mike Petrov
Greenhorn
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More like the 0.2 of the 80/20!

and you’re welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic