• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic