Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question" Watch "a question" New topic
Author

a question

Andrei Pop
Ranch Hand

Joined: Oct 08, 2004
Posts: 43
Look at the following piece of code:

String s1 = "abc";
String s2 = s1;
String s3 = "abc";

How many objects are created? My answer is: 2.

What do you think?

Regards,
Andrei
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Numer of Strings:-2
Number of character strings:-1
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Andrei,
I think there should be only 1 string literal object in the String constant pool.
[ November 09, 2004: Message edited by: Jay Pawar ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Andrei,

I agree with Jay.

You can verify that by using == operator to determine if s1 and s3 reference to the same object.



Joyce
[ November 10, 2004: Message edited by: Joyce Lee ]
Hai Le
Greenhorn

Joined: Sep 22, 2004
Posts: 28
as for me i think there is one object and 3 reference. Correct my thought if i'm wrong...!!


Step one step back to learn more thing..!!<br />Post to learn<br />learn from mistake<br />take whatever i understand...!!!<br /> <br />SCJP 1.4, Brainbench Java 1 & 2.<br />SCWCD 1.4 (preparing...!!)
Andrei Pop
Ranch Hand

Joined: Oct 08, 2004
Posts: 43
Thanks for answers.

The authors of the mock exam think there is one object only, the one in the constant string pool. But there are 2 string objects created.

Andrei
Nandish Sri
Ranch Hand

Joined: Jan 19, 2004
Posts: 45
Hi,
As I think there are 2 String Objects and 3 references are created,
1 String Object in String pool and One String Object in HEAP.
Correct me If I'm wrong !

Regards,
Nandish


Nandish
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
I believe object will be placed on heap only if it is created by using new keyword. I agree with thixtymilk... 1 string object and 3 references.


Do you still think there will be 100 objects ?
Since Strings are immutable, it is good way to save memory by just creating 100 references refering (pointing) to only 1 String literal object in String constant pool.
PNS Subramanian
Ranch Hand

Joined: Jul 13, 2004
Posts: 150
The following is from JLS

"Literal strings within different classes in different packages likewise represent references to the same String object. " - doesnt this mean that there is only one string object ?
Andrei Pop
Ranch Hand

Joined: Oct 08, 2004
Posts: 43
Jay, you are right. I knew about the string pool, but I sticked to the immutable behaviour of the String class. In the K&B book there's not extensive coverage for the case we discussed.

Thanks,
Andrei
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Andrei,
I am reading K&B book and I love the writing style of both authors and the way they have managed to present the facts. As a matter of fact, I gave my answer based on what I read from the same book. Here is the extract



To make Java more memory efficient,the JVM sets aside a special area of memory called the �String constant pool.� When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.


Page 359.


Hope this helps you.
 
Don't get me started about those stupid light bulbs.
 
subject: a question