This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Total number of object string creates? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Total number of object string creates?" Watch "Total number of object string creates?" New topic
Author

Total number of object string creates?

Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 68
Please confirm me, that how many string objects gets created totally for below code -

As per my understanding there will be two options:-
a) str, a,b,ab,c,abc (i.e. 6)
b) str, a,b,c,abc (i.e. 5)

Please confirm which one is correct and also let me know if i missed any other object.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I think only 1 object will be created and that is "abc" as a b and c are all literals, so the compiler will join them together at compilation...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
Sharad Golu wrote:Please confirm me, that how many string objects gets created totally for below code -

As per my understanding there will be two options:-
a) str, a,b,ab,c,abc (i.e. 6)
b) str, a,b,c,abc (i.e. 5)

Please confirm which one is correct and also let me know if i missed any other object.


I would say it will be "a", "b", "c" and "abc". The variable str is just a reference to "abc", won't be another object.


“Everything should be as simple as it is, but not simpler.” Albert Einstein
Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 68
Duc,
Agree that str is just a reference not any object..Thanks for making me clear.

but ideally a, b, c these 3 object gets created at compile time only in the string constant pool.
But at run time whether only abc gets created or ab will also gets created.

Is there is any way to check the total nos of objects created in heap?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40


I think Ankit is correct. The sum of three literals can be determined at compile time, hence, "abc" is in the string pool. And "a", "b", and "c", is not needed (and should not be created) at all.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
(1) There is no object got created at compile time, it just compiles java code to bytecode.
(2) The three object "a", "b", and "c" should be created at the class loading time, i.e. when JVM loads the class
(3) There should be no interim "ab" object in the modern JVMs, they should use some other less expensive way to concatenate the string with the + operator. So it should depend on specific JVM implementation.

My opinion is still the same: there will be only 4 string objects created.
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
Duc Vo wrote:(1) There is no object got created at compile time, it just compiles java code to bytecode.
(2) The three object "a", "b", and "c" should be created at the class loading time, i.e. when JVM loads the class
(3) There should be no interim "ab" object in the modern JVMs, they should use some other less expensive way to concatenate the string with the + operator. So it should depend on specific JVM implementation.

My opinion is still the same: there will be only 4 string objects created.


My bad, it does seem the compiler (at least with my eclipse 3.4 compiler running under JDK6) concatenates the string at compile time, hence there will be only one object.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Duc, the compiler is very smart, it looks for every opportunity to save memory and JVM's time. So when you write this



The compiler sees that unnecessary strings are being created and thus to save memory and JVM's time, the compiler would convert the statement to



And thus only one string will be created...
Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 68
Thanks Duc & Ankit for such a good explanation.

But say if i modify the code
String str = "a"+"b";
str = str+"c";

In this case i hope ab will also be created (i.e total 5 objects)
Correct me if i am wrong.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17



This code will create 3 objects i.e. "ab", "c" and "abc"...
Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 68
Hi Ankit,

In this case i completely got for ab & abc, but didn't understand this time why "c" is also created as object in heap.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Total number of object string creates?
 
Similar Threads
String pool and heap memory
String
equals() and ==, doubt
initializing an array of arrays
Another Question in GC