• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

String objects

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt on the number of string objects created in the following code. I got this from Kathy's programmer cert book.
The question is how many string objects are created in the following code

String s1 = "spring";
String s2 = s1 + "summer";
s1.concat("fall");
s2.concat(s1);
s1 += "winter";
System.out.println(s1 + " " + s2);

plz i need some detailed explanation!

thanks in advance
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashok,

I think 8 Object will get created.

four objects for literals --> "Spring","Summer","winter","fall"
four other objects for concat operation -->
1) s2 = s1 + "summer"
2) s1.concat(...)
3) s2.concat(...)
4) s1 += ...

I'm having a little doubt here.... while we print by S.o.p....does it create new String object or not...
if it creates then its 9 else 8

someone Please Clarify..

Regards,
Mausam
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = "spring"; // a reference is created in string pool
String s2 = s1 + "summer"; // a reference is created in string poool and one object is created in heap
s1.concat("fall"); // one object is created and it is available for Garbage collection
s2.concat(s1); // one object is created and it is available for Garbage collection
s1 += "winter"; // one object is created in heap
System.out.println(s1 + " " + s2); // one object is created in heap
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry , My above post is wrong.
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Krishna,

That post solved my problem. So Ashok, there will be 9 objects total.
If you draw "reference <---> object diagram u'll come to know with keeping one thing in mind that "String in JAVA is immutable.

Regards,
Mausam
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mausam,

Yeh you are right.

Regards,
krishna.
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I had doubt regarding this question

when using System.out.println

here we also using a literal " ".

I think this adds another object which has the reference in string pool.

In that case the total objects are 10.

plz Any one clarify this doubt..
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes u r right Hari..
10 objects are created there...

 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Yes Hari,

I had forgotten that point there...will take care about that...

Anyways...hope that Ashok has got his doubt cleared..

Regards,
Mausam
 
ashok devaram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai mahi,kkkad and all

String s1=new String("ashok");//1
String s2="reddy"//2

how many string objects are created at line 1?
how many string objects are created at line 2?
if created where they r created?
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashok,

In the first line 2 objects will get created. In the second line 1 object will get created.

The explanation is something like this,
When you specify a String as Literal, one object will get created in the heap and the reference will be put in the String pool. Then when you create a String object by "New" operator, specifically a new object will get created with the value provided in the argument.

String str = new String("mausam");

first "mausam" will get created and its reference will be placed in String literal pool when the class loads. And because of the "New" operator, a new object will get created and the reference will be given to "str".

in case of second statement, we dont have "New" operator. So, just reference of the String will be given.

Hope it will clarify your doubt, otherwise there is a good "Journal" post in JavaRanch itself, which will help you to clarify the concept.

Regards,
Mausam
 
ashok devaram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much kakkad.I got the point!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how come 10 objects?...

String s1 = "spring";
String s2 = s1 + "summer";
s1.concat("fall");
s2.concat(s1);
s1 += "winter";
System.out.println(s1 + " " + s2);

please explain line by line...
Thanks,
Bama.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hope this helps.
 
bama sabapathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Steve.

-Bama
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanh u very much steve, this time i got it write!
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People,
Objects are created in pool or heap. But, their references are created in stack. Am I right?
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right mani. Refer secition on "Local Variables" in K&B's chapter 2.
 
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic