• 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

String Object query

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... 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 ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dinesh Arora
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic