Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String objects

 
ashok devaram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sorry , My above post is wrong.
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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..
 
Mahendar Reddy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thank you very much kakkad.I got the point!
 
bama sabapathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hope this helps.
 
bama sabapathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Steve.

-Bama
 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanh u very much steve, this time i got it write!
 
Mani vannan
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People,
Objects are created in pool or heap. But, their references are created in stack. Am I right?
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right mani. Refer secition on "Local Variables" in K&B's chapter 2.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic