• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string pool

 
Kendall Ponder
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?
 
Roel De Nijs
Sheriff
Posts: 10662
144
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