|
![]() |
Originally posted by Christy Smith:
Am I right? Thanks
When the Java compiler encounters a String literal, it checks if the literal is already in the literal-pool. If it is there, then no new literal is created, just a reference is made to it. If it is not there, then new literal is created.
Ashik Uzzaman
Director of Engineering, Twin Health, Mountain View, CA, USA
Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Java lover from hell!
Ashik Uzzaman
Director of Engineering, Twin Health, Mountain View, CA, USA
But if we add another string using 'new' in heap (see line 4)does it not create a new object "world"?
Watch out the following code.
code:
--------------------------------------------------------------------------------
String s1 = "world"; //1
String s2 = "world"; //2
String s3 = new String("world"); //3
String s4 = new String("world"); //4
i would like to know whether concatenation of 2 strings produce a new String object.For eg:
String str="Good";
String str1=str+"Day";
Here in the above code,how many String objects are constructed?
According to me 3 objects.2 String literals "Good" and "Day" in String Pool.And 1 String object "GoodDay" in the heap.
kindly let me know if i am correct or not.Pls confirm if the String "GoodDay" is created in heap or in String Pool.
Ashik Uzzaman
Director of Engineering, Twin Health, Mountain View, CA, USA
Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Ashik Uzzaman
Director of Engineering, Twin Health, Mountain View, CA, USA
String str="GoodDay"; //1
String str1="Good"; //2
String str2=str1+"Day"; //3
str.equals(str2) returns true.
str==str2 returns false.
Therefore, in line 3 a new String object is created in the heap and reference of that object is assigned to str2.
str2 doesn't refer to "GoodDay" in the String Pool.
Ranchers pls comment on this.Tell me if my thinking is correct or not.
rajashree.
But i am still confused as to where "GoodDay" String object will be created,whether in the heap or in the String Pool?also will there be 2 "GoodDay" String objects,one in Pool and one in heap?
in line 2 of the code new String()is not used just + operator for concatenation.
kindly throw light on the above query.
Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
Strings computed at run time are newly created and therefore distinct.
The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
Generally this new String will not be in the pool unless you explicitly call intern() to put it there
Originally posted by Ashik uzzaman:
Guoqio, IMHO zero (0).
Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Ashik Uzzaman
Director of Engineering, Twin Health, Mountain View, CA, USA
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |