• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

string pool  RSS feed

 
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output from the following code is: true false

I take that to mean replace forms a new string object but does not put the object in the string pool in the same way new String("Test"); would not put the "Test" string in the string pool. Am I correct?
 
Sheriff
Posts: 11338
178
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only String literals (compile time constants) are added to the String Constant/Literal Pool (hence the name of the pool ).

So both replace methods create a String at runtime and are not added to the String Constant/Literal Pool. That's why you'll get false on line9.

If you want to add a String created at runtime to the String Constant/Literal Pool, you could use the intern method.

Hope it helps!
Kind regards,
Roel

(Disclaimer: you don't need to know the intern method for the OCA exam, I just mentioned it for completeness)
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!