File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on String: how many objects are created?

 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vicken
 
VP Jain
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yeah i am sorry, what u say is correct. I made a mistake...
 
Lukas Alamar
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic