• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

String pool question

 
Ranch Hand
Posts: 134
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that Java maintains a string pool and it interns a new string literal for performance reason. That explains why the following statement returns true,



But then why the following code returns false? What mechanism is at work here?

 
Marshal
Posts: 7847
538
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make str0 final and that would return true, now, think what that may tell you?
 
Marshal
Posts: 70997
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is more information in this part of the Java® Language Specification (=JLS) and our very old JavaRanch Journal article by Corey McGlone called, “Strings, literally”. Try here.
 
Greenhorn
Posts: 2
MyEclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
REMEMBER;
String a = "you cannot change me";

String b = "you cannot change me";

system.out.println(a==b); // outputs true [ because 'a' is from String pool and similarly 'b' is from String pool and they both are pointing to same object "you cannot change me"
Now;

String c = "you cannot" + "change me";

system.out.println(a==c); // outputs true [HANDLE WITH CARE, because "you cannot" and "change me" both are constants; they will combine at COMPILE TIME and would give "you cannot change me" and hence compiler saw   them combined as "you cannot change me" and points to the same object as in String Pool]
String d = "you cannot";

String e = d + "change me";

system.out.println(a==e); // outputs false [TAKE SPECIAL CARE, here 'd' is variable and hence both 'd' and "change me" would combine at RUNTIME [not compile time] and hence 'e' is referring  to a new object ]

BTW, system.out.println(a.equals(d)); outputs true [at runtime values are same]

if we had ;
final String f = "you cannot";

String g =  f + "change me";

system.out.println(g==a); // outputs true [because 'f' is final hence it is also constant we cannot change its value therefore it acts like constant and will combine to "you cannot change me" at compile time]
 
Campbell Ritchie
Marshal
Posts: 70997
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ADB: that looks correct and welcome to the Ranch

Of course you can forget all about the String pool for about 99.9% of your programming. There is only one circumstance when you really need to know it: when sitting a cert exam
 
Quazi Irfan
Ranch Hand
Posts: 134
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I get false since str2 is assigned a value during runtime, and to have it in the String pool, I have to intern it.

So, I tried the following and it is working even if things appears out of order,



The output returns true, since "HelloWorld" is in the string pool. But isn't it weird that the interning operation occurs before placing "HelloWorld" to the string pool? The code still returns true when I move line 4 at the top.

Is it an indication that the compiler is going over the code over multiple passes, and "HelloWorld" is already in the pool before entering operation takes place in line 3?
 
Campbell Ritchie
Marshal
Posts: 70997
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Quazi Irfan wrote:. . . Is it an indication that the compiler is going over the code over multiple passes

No. The intern() call and the == test are executed at runtime.

. . . "HelloWorld" is already in the pool before entering operation takes place in line 3?

Yes, that String object is put into the String pool when the class is loaded. It's called interning not entering.
 
Quazi Irfan
Ranch Hand
Posts: 134
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To reiterate, during compilation "HelloWorld" is put in the String pool, and during runtime, intern() is executed and results in str2 pointing to the "HelloWorld" string that was already in the string pool. Am I correct?
 
Campbell Ritchie
Marshal
Posts: 70997
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic