This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String objects" Watch "String objects" New topic
Author

String objects

ashok devaram
Greenhorn

Joined: Oct 25, 2005
Posts: 6
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

Joined: Oct 27, 2005
Posts: 40
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

Joined: Nov 08, 2005
Posts: 110
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

Joined: Nov 08, 2005
Posts: 110
Sorry , My above post is wrong.
Mausam M Kakkad
Ranch Hand

Joined: Oct 27, 2005
Posts: 40
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

Joined: Nov 08, 2005
Posts: 110
Hi Mausam,

Yeh you are right.

Regards,
krishna.
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
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

Joined: Sep 27, 2005
Posts: 73
Yes u r right Hari..
10 objects are created there...



SCJP- 98%<br />SCWCD-92%
Mausam M Kakkad
Ranch Hand

Joined: Oct 27, 2005
Posts: 40
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

Joined: Oct 25, 2005
Posts: 6
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

Joined: Oct 27, 2005
Posts: 40
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

Joined: Oct 25, 2005
Posts: 6
Thank you very much kakkad.I got the point!
bama sabapathi
Greenhorn

Joined: Apr 05, 2002
Posts: 8
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

Joined: May 22, 2003
Posts: 657


Hope this helps.
bama sabapathi
Greenhorn

Joined: Apr 05, 2002
Posts: 8
Thank you Steve.

-Bama
achayya matta
Ranch Hand

Joined: Oct 27, 2005
Posts: 111
Thanh u very much steve, this time i got it write!
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
People,
Objects are created in pool or heap. But, their references are created in stack. Am I right?


Manivannan
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1511
You are right mani. Refer secition on "Local Variables" in K&B's chapter 2.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String objects
 
Similar Threads
String objects ..
how many String objects are created
Strings
How many String objects are created.
How many String objects are created in the code