Reputation: 69
I want to clarify a doubt here
when we create a string via new operator
String s=new String("jk");
jk goes in String constant pool and heap
Now see
String s1=s+"winter";
//case1
or
String s1="jk"+new String("winter");
Does winter in both case go to String Const pool?
How do i check it?
Upvotes: 0
Views: 50
Reputation: 36304
Yes. In both cases "winter"
will go into the String constants pool. The JLS specifies that anything between ""
is considered as a compile-time constant and goes into the String constants pool.
Note : new String("winter")
--> is redundant and almost always considered evil
Upvotes: 2
Reputation: 3035
Only String constants like "abc"
, or expressions that the compiler can evaluate during compile-time, like "abc" + "def"
, or strings that you call intern()
on go into the constant pool.
Strings that result from an expression evaluated during runtime are just regular objects.
Upvotes: 1