• 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

Total number of object string creates?

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

This code will create 3 objects i.e. "ab", "c" and "abc"...



Based on the earlier explanation its two right? "ab" and "abc"
 
Harshana Dias
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshana Dias wrote:

Ankit Garg wrote:

This code will create 3 objects i.e. "ab", "c" and "abc"...



Based on the earlier explanation its two right? "ab" and "abc"



Well I later read that, "Generally using JDK 1.6 and above the compiler will automatically join strings together using StringBuilder."
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshana Dias wrote:

Ankit Garg wrote:
This code will create 3 objects i.e. "ab", "c" and "abc"...



Based on the earlier explanation its two right? "ab" and "abc"



It's three. Ankit is correct.

Basically, the second line of code is executed at runtime. The strings "ab" and "c" are concatenated to form the "abc" string at runtime.

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String Str = "a" +"b"+"c";

Only one object will be created in scp area.

Bcs all are constants , at compile time all three joins together with help of '+' operator , it's will become "abc" so now it's consider as one string and a single object will be created in scp area ..
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic