• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Total number of object string creates?

 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code will create 3 objects i.e. "ab", "c" and "abc"...
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic