• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there any cases when Strings will not go in String Constant Pool  RSS feed

 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any cases when Strings will not go in String Constant Pool or in both the cases it will go in 'String constant pool'

 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only String Literals will be stored in Pool.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shahir Deo wrote:Only String Literals will be stored in Pool.


And Strings on which we explicitly call intern().
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:Is there any cases when Strings will not go in String Constant Pool or in both the cases it will go in 'String constant pool'



The String "abc" exists in the constant pool before that code is executed.

The first line just gets a reference to that existing String and copies it to variable s1.

The second line also gets a reference to that String, but then creates a new String copied from it, and returns a reference to that new String. The new String is not placed in the constant pool.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!