File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q on String: how many objects are created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q on String: how many objects are created?" Watch "Q on String: how many objects are created?" New topic
Author

Q on String: how many objects are created?

Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi there,
given the following question:
How many String objects will be created when this code is executed?

String s1= "abc" ;
String s2= new String("xyz");
s2=s1;
s1.toUpperCase();
String s3="abc";
String s4=s3.replace('a','A');

The Answer to this question according to the exam simulator I
use is 4. (one String object in line 1,2,4, and 6)
I think it should be 5 because the line String s2 = new String("xyz");
creates two objects one in the normal (nonpool) memory and the other
in the pool memory (i.e. for the literal "xyz").
Anyone can comment this?
Thanks,
Gian Franco
[ March 21, 2004: Message edited by: Gian Franco Casula ]

"Eppur si muove!"
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Gian-
If Bert was here he would say this:

Hello everyone!
I just want to clarify for everyone what's on the exam and what's NOT!
Understanding String immutability IS on the exam, and it's big!
Understanding when (non-String), objects are eligible for GC IS on the exam, and it's big and complicated!!!
However, because the String constant pool is a bit vague, the exam WILL NOT ask you to know when String objects are eligible for the GC.
So I'd recommend that you start this thread all over again using objects of a type other than String - otherwise this whole thread is confusing and misleading!

Right Bert?
[ March 21, 2004: Message edited by: Vicken Karaoghlanian ]

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Vicken,
I don't get your point.
The question I posted does not address GC,
it's about object creation within the
fundamental classes in the java.lang package.
And additionally it's part of the Whizlabs
package that ought to give a reasonable
indication of what to expect in the exam.
Greetings,
Gian Franco
[ March 21, 2004: Message edited by: Gian Franco Casula ]
VP Jain
Ranch Hand

Joined: Feb 11, 2004
Posts: 81
Hi,
But how many String objects will be lost in your code ?
Now tell me in the following code how many String objects will be created before the println statement in the following code
String s1 = "winter";
String s2 = s1 + "summer";
s1.concat("fall");
s2.concat(s1);
s1 +="winter";
System.out.println(s1 + " " + s2);


SCJP SCWCD SCBCD <br /> <br />what u do in life echos in eternity!
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi there,
Originally posted by Vijayakeerthy Parsuvanath:
But how many String objects will be lost in your code ?
Now tell me in the following code how many String objects will be created before the println statement in the following code

Why answer a question with a question? :roll:
I'd say 7 String objects for your question (before
and not including the println statement)
I would appreciate if you could answer my original
question.
Greetings,
Gian Franco
[ March 22, 2004: Message edited by: Gian Franco Casula ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Ooops!!! I haven't read your question clearly, my apologies.
In Java, all the new()'ed objects are in the heap, including new()'ed String and StringBuffer. A pool on the other hand is where constant Strings are.

I think it should be 5 because the line String s2 = new String("xyz");
creates two objects one in the normal (nonpool) memory and the other
in the pool memory (i.e. for the literal "xyz").

IMHO, there is only one object created at line 2 and that object is located in the heap.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Thanks Vicken
VP Jain
Ranch Hand

Joined: Feb 11, 2004
Posts: 81
Hi,
Sorry to say that u are wrong because there will be a total of eight
String objects created as follows. "winter","winter"(lost),"winter summer",
"fall"(lost),"winter fall(lost),"winter summer winter(lost),"winter(lost),
"winter winter" . Only two of eight String objects are not lost in this
process.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Dear Vijayakeerthy,
I'm afraid your calculation has some
mistakes. First look at K&B(pg359)
you will see a similar example, with a
proper explanation. Secondly to come to
your mistakes:
String objects created as follows. "winter","winter"(lost),
"winter summer","fall"(lost),"winter fall(lost),"winter summer winter(lost),
"winter"(lost),"winter winter" .

mistake 1.) "summer" is lost and not "winter
mistake 2.) "winter" is not created again after
the first line, it's allready in the string
literal constant pool.

My calculation was as follows:

String s1 = "winter"; //1
String s2 = s1 + "summer"; //2
s1.concat("fall"); //3
s2.concat(s1); //4
s1 +="winter"; //5
System.out.println(s1 + " " + s2);//6

line 1: "winter"
line 2: "summer" "wintersummer"
line 3: "fall" "winterfall"
line 4: "wintersummerwinter" (no additional "winter" string constant,
becauses it's allready in the string constant pool)
line 5: "winterwinter"
line 6: (not included in calculation)
Originally posted by Gian Franco Casula:
...(before and not including the println statement)

Best regards,
Gian Franco Casula
[ March 22, 2004: Message edited by: Gian Franco Casula ]
atiqur rahman
Greenhorn

Joined: Aug 30, 2003
Posts: 10
about the original question:
String s1= "abc" ; 1
String s2= new String("xyz"); 2
s2=s1; 0
s1.toUpperCase(); 1
String s3="abc";0("abc" is already created)
String s4=s3.replace('a','A');1
so, i think it is 5
VP Jain
Ranch Hand

Joined: Feb 11, 2004
Posts: 81
Hi,
Yeah i am sorry, what u say is correct. I made a mistake...
Lukas Alamar
Ranch Hand

Joined: Mar 17, 2004
Posts: 68
Hi all, answering the original question:
4 is correct!
4 String objects will be created when the following code is executed:
creates 1 obj in the pool
creates 2 objs:
creates a new String obj in normal (nonpool)
memory, in addition the literal "xyz"
will be placed in the pool
creates 1 obj in the pool
there are 4 objs created so far

[ March 23, 2004: Message edited by: Lukas Alamar ]

SCJP 1.4
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Ciao Lukas,

You forgot the string created by

this one is created, but lost since
there is no reference to it.
Greetings,
Gian Franco
Lukas Alamar
Ranch Hand

Joined: Mar 17, 2004
Posts: 68
oops! u're right man
...che figuraccia...
[ March 23, 2004: Message edited by: Lukas Alamar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q on String: how many objects are created?