• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Handling

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
String str="java";
String str1=new String(str);
String str2=new String("java");
i would like to confirm how many String objects are getting created in the above piece of code?
According to me,3 objects.
am i correct?kindly comment.
rajashree.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep 3.
"new" creates a seperate object.
If you had:
String str="java";
String str1= "java";
String str2=new String("java");
then str and str1 would reference the same object and you would only have 2 String objects.

[This message has been edited by Guy Reynolds (edited August 17, 2001).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct me if I am wrong.
I thought Java compiler creates a String literal (object) when it sees "java" in the statement String s = new String("java"); then, for that particular statement, two objects are created. However, since we had a statement String s = "java" before, the second "java" was not recreated, just pointed to the literal pool; therefore, only one created in this case. But if we did not have the previous statement, String s = new String("java") itself would have created two objects.
Am I right? Thanks
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christy Smith:

Am I right? Thanks


Yes, correct.
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.
This is true whether a String is created using new or not.
So if we have the following statements:
String s1 = "hello"; //1
String s2 = "hello"; //2
String s3 = new("world"); //3

then in //1, a literal "hello" is created in the pool by the compiler. This literal is internally represented by a String object. So this is in deed a creation of a String object.
In line //2, when the compiler encounters "hello", it finds that it is already in the pool. So just a reference is made and no object is created.
In line //3, the compiler creates the literal "world" in the pool. So again an object is created.
Now at run time, line //3, alone has new keyword. So a new String object "world" is created in the heap (not in the pool) and s3 is made to point to that. So here again one new object is created, but in the heap.
This makes a total of 3 objects
2 in pool - "hello" & "world"
1 in heap - "world"
Now if the third string, s3 were created as follows:
String s3=new String("hello");
Then, the compiler will not create a new literal "hello" again in the pool as it is already there. So only at runtime a new object will be created in the heap.
This means that the following code will just create 2 objects
1 in pool - "hello"
1 in heap - "hello"
String s1 = "hello"; //1
String s2 = "hello"; //2
String s3 = new("hello"); //3
Hope the explanation is clear,
Gaja.


 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, so in rajashree's example there is 1 object in the pool and 2 on the heap... 3.
And in mine there is one in the pool and 1 on the heap... 2.
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.


I agree with u Gaja that only 3 objects are created in ur code including String literals. 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.


------------------
azaman
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aazman, I think the code you provided, creates 3 objects, is that right?
--Farooq
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 objects were created and 1 is in pool and the other 2 are in heap.Am i right?
------------------
Java lover from hell!
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

This is an excerpt from my last post.Kindly tell me in the above piece of code how many String objects are created?i am confused about the String "GoodDay".pls tell me how many of "GoodDay" String objects are created and where,in String Pool or heap or in both?
rajashree.

[This message has been edited by rajashree ghatak (edited August 20, 2001).]
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx Farooq, Metal, Rajasree!
My thinking (in following two parts)is like that-----
[PART-1]
When u r creating strings like String str1 = "Good";, u r creating String literals in String pool without any duplication of the same string. We all know that String str2 = "Good";, will not create any new String literal in the pool. But when u r creating String str3 = new String("Good");, u r creating String objects in heap without checking the string in pool.
But if u create another string object by String str4 = new String("Good"); then u r actually creating a whole new string object whether there is another object in the heap with same object or not. But this is i m thinking what and need ur thoughts....
[PART-2]
Also i have another question that whether creating with String str5 = new String("Bad");, before "Bad" is being created in the heap, it is first created in the String pool and then a string object is being created in the heap with this value or not?
Ranchers plz all give ur opines to this thread.....
------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 19, 2001).]
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i have by mistake edited my last post.i intended to quote a portion from my last post.kindly look at my last post and throw some light on my doubt.
Thanx in advance,
rajashree.
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Azaman,ur thinking in ur last post of this thread in correct.
Both the parts.
rajashree.
 
Gaja Venkat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashik:
Let me just copy-paste the relevant part of the code once again below to make clear which code we are discussing now!
Your question is also quoted below.



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


As I said in my previous post in this thread, compiler creates literals (internally represented as String objects) in the String pool without creating duplicates.
So //1 will create a String object "world" in the pool. //2 will not create a new object in the pool as it is already there. So execution of //2 will just result in making s2 point to the already existing object in pool.
Now when //3 is encountered, compiler will not create any object in pool when it sees the literal "world" as it is already there. But at runtime, due to the oprator new, a new String object is created in the heap.
The same happens for //4 also. That is, when //4 is executed, no object is created in the pool, but at runtime, a new String object is created in the heap.
So totally 3 objects are created.
1 - pool (when //1 is executed)
2 - heap (when //3 & //4 are executed).
Farooq & Metal are right!
In short, whenever an object is created using new opeartor a new object is created in the heap, immaterial of whether another object with same value is already there or not.
Hope this is clear,
Gaja
Sun Certified Programmer for Java 2 Platform
 
Gaja Venkat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajashree:
I am copy-pasting your question below again:



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.



Let me add comments to the code as follows for clear reference:
String str="Good"; //line 1
String str1=str+"Day"; //line 2
You are correct. Two objects in pool & one in heap are created.
When //line 1 is executed, the literal "Good" is created in the pool. When //line 2 is executed, there is a reference, str and there is a String literal "Day" on the right hand side. The literal "Day" is created in the pool. Now at run time, str+"Day" results in a new String object. This is created in the heap.
So totally 3 objects are creates
2 in pool ("Good" & "Day")
1 in heap ("GoodDay")
Hope this is clear.
Gaja
Sun Certified Programmer for Java 2 Platform
(Had gone for this yesterday, hence the delay in replying!)
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Venkat
read ur explanation to my query.thanx for the posting.
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.
i would also ask other Ranchers to comment on this.
rajashree.

 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajashree,
When you write the following:
String c = string1 + string2;
The last line is changed by the compiler to look more like this:
String c = new StringBuffer().append(string1).append(string2).toString();
I think a cool optimizer would be allowed to recognize

and translate it to

if it wants.
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gaja, Yah i was exactly thinking in that way u last posted. And Rajashree. remeber u r creating new Objects each and every time u r new operator. Don't confuse with the String literal (object) in the string pool and string object in heap.
------------------
azaman
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i haven't yet got a clear explanation to my query regarding concatenation of Strings.after trying out a small code on computer i found out that in the below given code following is the result:
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.

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all, after the following method finished, how many object are eligible for GC?

Thanks in advance.
Guoqiao
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guoqio, IMHO zero (0).
------------------
azaman
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guoqiao,according to me 1 String object("GoodDay") will be eligible for GC.
Ranchers kindly comment on my previous post in this thread and let me know whether my thinking is correct or not.

 
Gaja Venkat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajashree:
Your answer in your previous post is correct. You had asked as follows:


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.



Here there are 4 objects totally:
3 in pool ("GoodDay" when //1 is compiled, "Good" when //2 is compiled & "Day" when //3 is compiled) and
1 in heap ("GoodDay" at run time as a result of execution of //3)
I think this is what I tried to explain in my previous posts too.
But in another post you had asked a question:


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.


The code under discussion when this question arose was

String str="Good"; //line 1
String str1=str+"Day"; //line 2

Here as I said earlier, 3 objects are created
2 in pool - ("Good" & "Day" during the compilation of //line 1 & //line 2 respectively)
1 in heap - ("GoodDay" at run time when //line 2 is executed)

When new is used, a new object is created in the heap. But I didn't mean to say that String objects will be created in heap only when new is used! The + operator in String does concatenation of strings and the important point is that it returns a new String. This new String object is created in the heap normally. Since in // line 2, we are assigning str1 to this new String, it will point to this in the heap.

Please note that the above sentence "that a new String is created in the heap normally" is based on my reading in Java Language Specification (JLS) & Robert, Hellers & Ernest (RHE )Book.
JLS (section 3.10.5) clearly tells as follows:


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 same section also tells that


The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.


This means by using a method called intern(), the effect of creating a literal in pool can be achieved.
Now look at what RHE tells in the situation when a String operation results in a new String.


Generally this new String will not be in the pool unless you explicitly call intern() to put it there


I thought of including all these quotes earlier, but then discarded the idea as the post would have grown very lengthy.
JLS section 3.10.5 has a very good example, with results & excellent explanation. That should certianly be helpful in knowing about String handling.
Hope this explanation helps to clarify your doubts.
Gaja.

[This message has been edited by Gaja Venkat (edited August 21, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wonderful replies. Thanks Everybody.
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Ashik, it seems the answer should be one object eligivle for GC since the //3 created a new String by the + operator.
But i am not 100% sure of it.
Someone else please confirm it.
Guoqiao

Originally posted by Ashik uzzaman:
Guoqio, IMHO zero (0).


 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Venkat for ur detailed explanation.
My doubt has cleared.
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guoqao,
Just see the Gaja Venkat's last post (seems as a documentation to be collected ) and everything will be cleared. Ur answer is right and i was wrong.

String str="GoodDay"; //1
String str1="Good"; //2
String str2=str1+"Day"; //3

Total string objects in Heap will be 1 and string literal objects in pool 2. So only the "GoodDay" in heap will be available for gc after the method finishes execution. I did not know that in line 3 the runtime String object will be created in Heap and only compile time string literals r worth to go for String pool (when not using 'new' op). Thnx Gaja for a detailed explanation.

------------------
Muhammad Ashikuzzaman (Fahim)
When you learn something, learn it by heart!
 
reply
    Bookmark Topic Watch Topic
  • New Topic