• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

String Objects

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Folks,

My Anser is B, But given answer is C. and i could't understand the statement." Line 13 creates two, one referred to by x and the lost String �xyz� ", where is the last string "xyz".Pls explain to me.

Question:-

3. Given the following,
13. String x = new String("xyz");
14. y = "abc";
15. x = x + y;
how many String objects have been created?
A. 2
B. 3
C. 4
D. 5
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


13. String x = new String("xyz"); //one "xyz" is created in string pool, new String("xyz") will create another string
14. y = "abc"; //one "abc" is created in string pool
15. x = x + y; //one "xyzabc" is created

 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Yes, You are correct in K&B also explained the same.

Here is the question from whizlab, How many String Objects?.
whizlab given answer is 4. but my answer is 5. pls correct me if am i wrong.

Thanks, Raghu.K

1. String s1 = "abc";
2. String s2 = new String("xyz");
3. s2 = s1;
4. s1.toUpperCase();
5. String s3 = "abc";
6. String s4 = s3.replace('a','A');
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls somebody exaplain to me.

Thanks, Raghu.K
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better, different question, different topic.

I would also count that five are "created":

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If, on line 5, you had String s3 = "xyz"; then there would be no new String object created on line 6 (see API for String.replace(char, char)).
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Better, different question, different topic.

I would also count that five are "created":



String s2 = new String("xyz");

I agree, total 2 objects will be created. But I doubt that both are string object...

:roll:
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I doubt that both are string object...



... because ...
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:


... because ...



I mean, string objects are created on string literal pool. So if both are string object then which one will be on garbage collection heap and refer to other one which is on string literal pool...

Does it make sense?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rathi, take another look at Corey's String Article, especially the second pictorial diagram.

There is a difference though, there is no "one" variable in the local variable table.
[ August 20, 2006: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how can a variable refer to two distinct (String) objects at the same time.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.String s1 = "abc"; // first string
2String s2 = new String("xyz"); // second and third string
3. s2 = s1;
4. s1.toUpperCase(); // fourth string
5. String s3 = "abc";
6. String s4 = s3.replace('a','A'); // fifth string
in line 1 ,one object is created in heap and its reference is stored in string literal pool.this operation occurs at time when class is loaded in memory,as string literals are handled like constants at loading time.At runtime this reference is copied into local reference

in line 2 a new string object is created in heap and s2 refer to it,this operation occurs at runtime ,only 1 string object is created

in line 4 a new string object is created

in line 5 a local reference is created to object created in line 1,by copying into it reference from string literal pool,no new object is created,as string literals are interned ie.only 1 object is made for all string literals having same contents

in line 6 ,a new object is created
---------so total 4 objects are created
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Mandeep.
I felt whole point lies at the line 2.Normally it creates two objects on the heap.By assigning s2 to s1 changing the picture at line 3.After line 3 s2 should be considered as a normal string (I mean its not created using 'new' any more ).
So at run time we have only one "xyz" object on the heap.
 
reply
    Bookmark Topic Watch Topic
  • New Topic