Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# String Objects

RAGU KANNAN
Ranch Hand
Posts: 103
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

wise owen
Ranch Hand
Posts: 2023

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
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
Pls somebody exaplain to me.

Thanks, Raghu.K

Barry Gaunt
Ranch Hand
Posts: 7729
Better, different question, different topic.

I would also count that five are "created":

Barry Gaunt
Ranch Hand
Posts: 7729
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)).

ankur rathi
Ranch Hand
Posts: 3830
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
But I doubt that both are string object...

... because ...

ankur rathi
Ranch Hand
Posts: 3830
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
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 ]

arunz netizen
Greenhorn
Posts: 1
But how can a variable refer to two distinct (String) objects at the same time.

Mandeep kaur
Greenhorn
Posts: 8
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

nivas ratthaaq
Greenhorn
Posts: 18
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.