• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

From Velmurugan's Mock Exam

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How many String objects are created when we run the following code.
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
Answer is 3
please explain
Thanking you,
Santhi
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 3 String Objects.
s1 = "Hello"; --- 1
s2 = s1;
s3 = s2 + "Pal"; ---2
s4 = s3;
s3 = new StringBuffer().append(s2).append("pal").toString(); --- 3
Regards
Siva.

[This message has been edited by Sivalingam Sivasuthan (edited February 07, 2001).]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the three objects in the object heap are :
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;----------------------------1
s3 = s2 + "Pal";--------------------2
s4 = s3;----------------------------3
From my understanding, there are only two conditions that an object will be pointed to the string pool
1. When we assigning a string literal to the String object
2. When we assigning String.valueOf(..) to the String object
Correct me if I am wrong.
KK.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pointing to objects in string pool and CREATING new objects is not the same.
3 new objects are created, thus, in these statements only:
s1 = "Hello"; //"Hello"
s3 = s2 + "Pal";//"Pal" and s2+"Pal"
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this question has been discussed before. the string literal "pal" is created as a temporary object - like a transient.
if the question had asked how many objects are there at the end, the answer would have been 2.
 
santhi Bendapudu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for clearing things.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody..
now answer is 2 or 3..???
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's 3.
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic