aspose file tools*
The moose likes Java in General and the fly likes String Object query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Object query" Watch "String Object query" New topic
Author

String Object query

Dinesh Arora
Greenhorn

Joined: Apr 18, 2007
Posts: 21
Hi,
I have a doubt in the following question:

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

According to me, Three String objects are created - 1 - abc(referneced by x) , 2- abc(refernced by y) , 3- xyzabc (refernced by x, the previos one "xyz" is now lost).

However in the explanition it says 4 string objects are created -
"ANS C. Line 13 creates two, one referred to by x and the lost String �xyz�. Line 14 creates one
(for a total of three). Line 15 creates one more (for a total of four), the concatenated String
referred to by x with a value of �xyzabc�."

Can anyone explain. this is a question from K&B certification book.

Thanks,
Dinesh
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

x = "xyz";

creates one String for "xyz"

x = new String( "xyz" );

creates that one, and we explicitely create another with the "new". This form of new String is wasteful and never necessary. I think.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Scott Escue
Ranch Hand

Joined: Jan 20, 2005
Posts: 34
Dinesh,

I'm with you, I see three Strings (say that ten times fast!)... but I've been wrong before

String x = new String("xyz");
This should create one String object

y = "abc";
This would create the second String object

x = x + y;
... and concatenating the two together should be a third
Scott Escue
Ranch Hand

Joined: Jan 20, 2005
Posts: 34
... and I think I'm wrong again! Stan, I should have read your explanation more closely before I opened my mouth. Since the String constructor takes a String instance as an argument then it seems that, as far as the compiler is concerned,

would be the same thing as
If that thinking is correct, then the second example makes it obvious that two String objects are created in the first assignment.

[ April 18, 2007: Message edited by: Scott Escue ]
[ April 18, 2007: Message edited by: Scott Escue ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

each unique string literal creates a string. so, "xyz" and "abc" both create one. If the same literal is used a second time, there will still be only one string created.

each time you see "new String()", guess what? a new String is created.

each string operation that creates a new string will, well, create a new string. so the "x + y" will have to create one.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dinesh Arora
Greenhorn

Joined: Apr 18, 2007
Posts: 21
Hi,
Stan Thanks for clearing out the doubt. So that means, when I say

String x= "abc" // creates one String object

while

String x = new String("abc") // creates two String object, the first one is new String() , the object created from a default String constructor which is eventually lost and the second is "abc" which is referenced by String variable X. Am I correct in what I have concluded??

Thanks,
Dinesh

[ April 18, 2007: Message edited by: Dinesh Arora ]
[ April 18, 2007: Message edited by: Dinesh Arora ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

String x = new String("abc") // creates two String object, the first one is new String() , the object created from a default String constructor which is eventually lost and the second is "abc" which is referenced by String variable X. Am I correct in what I have concluded??


No. The literal "abc" is one String object. Then you create a new String object by calling the constructor of class String that takes a String as a parameter (not the 'default String constructor' - if you mean the constructor of String that takes no arguments). That new String object is initialised by copying the contents of the literal string "abc" to it. The variable 'x' will point to that string.

Note that 'x' will not refer to the String object created for the literal string "abc" - it will refer to the String object that you explicitly created.
[ April 19, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
You are all correct; when you say String s1 = "abc";, then one String object is created and is placed in the String pool, which is a special pool, different from the normal object heap. So, all the typed in String objects, that is, those String objects, which are created without using the keyword new, are added to this pool.

And when you say String s2 = new String("abc");, one object is created on the normal heap and the variable s2 is made to refer it, while the String object "abc" is placed in the String pool, which can be used later when a new String object is created with same sequence of characters. I guess that's the reason why String objects were made immutable.
So that makes a total of 2 objects in the JVM memory.

Njoy!!
Sid
[ April 19, 2007: Message edited by: Sidd Cool ]
Dinesh Arora
Greenhorn

Joined: Apr 18, 2007
Posts: 21
Thanks Every one, I know I wont go wrong on this when I appear for my SCJP next month.

Thanks,
Dinesh
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I seriously try to avoid thinking about the special magic of Strings because it so rarely matters. But here's one time it might. JVM gurus help me if I muff this up:

Let's say these are created by different teams and distributed in separate jars. A must be created first and made available to the B team. Then B builds their jar and deploys the app with both jars.

Now team A changes HELLO to say "Greetings and salutations!" They deploy their jar, stop and restart the app. The B team doesn't even know about the change so they don't recompile or deploy. What gets printed?
[ April 20, 2007: Message edited by: Stan James ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Sidd Cool:
when you say String s1 = "abc";, then one String object is created and is placed in the String pool, which is a special pool, different from the normal object heap.


That's not right, is it? The String objects supporting string literals go in the one and only (*) Java heap. The String Pool stores references to these String objects.


(*) Well, it's split into "generations" for GC purposes, but that's another subject


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The "pool" consists of two parts: a hashmap-like table which stores references to all the pooled Strings, and the Strings themselves. The table is not part of the heap; it's part of the JVM internals. (It's a hash map, but not a HashMap.) The strings themselves are indeed stored in the heap - specifically, they're stored in a special part of the heap, namely the permanent generation. (Where "permanent" is a slight exaggeration, since things can be GC'ed from there under certain circumstances, but it's generally rare.) So the answer is sort of a combination of what Sidd said and what Peter said. In practice though, there's little reason for most developers to care where the Strings are really kept.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String Object query
 
Similar Threads
Object creation
Definition of a Java object
How many string objects ?
How many String objects have been created?
String Objects