• Post Reply Bookmark Topic Watch Topic
  • New Topic

String concatenation & literal pool  RSS feed

 
Sree Aniyappan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a program like below.




On running, it prints "s6 and s7 are same". But if i remove the final modifier of s1 and s2 then it's not. What could be the reason?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you mark the String as final, then your catenation becomes a constant expression, (also called compile‑time constant) and is eligible for automatic entry to the String pool.
If you take the final modifier away, it ceases to be a constant expression.
 
Sree Aniyappan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Campbell..!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!