| Author |
Question on String objects
|
Yaroslav Ashurin
Greenhorn
Joined: Apr 25, 2008
Posts: 18
|
|
Dear All, Could you please help me out with the question concerning String pool. Fragment: public String makinStrings() { String s = "Fred"; s= s + "47"; s = s.substring(2, 5); s = s.toUpperCase(); return s.toString(); } How many String objects will be created when this method is invoked? My count is 4: "Fred", "Fred47", "ed4" and "ED4". But "Right answer" is 3. What's wrong with it? Thank you for any help.
|
SCJP 1.5
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
There are 5 String objects in that method. "Fred" and "47" are created when the class is loaded. When you run the class you create 3: "Fred47" substring(2,5) "ed4" and that to upper case, "ED4". String s = "Fred"; does not create a new object; it simply adds a name (s) to it.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
And welcome to the Ranch
|
 |
Yaroslav Ashurin
Greenhorn
Joined: Apr 25, 2008
Posts: 18
|
|
Thank you indeed, Campbell! You say "class is loaded". Did you mean "method"? [ April 25, 2008: Message edited by: Yaroslav Ashurin ]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
You're welcome
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Yaroslav Ashurin: You say "class is loaded". Did you mean "method"?
No, he meant class. All String literals in a class are created when a class is loaded. So "fred" and "47" will be created before the method is called and even if the method is never called.
|
Joanne
|
 |
Yaroslav Ashurin
Greenhorn
Joined: Apr 25, 2008
Posts: 18
|
|
|
Now I've got the picture... There are 5 String objects in the String pool, 2 of them are created upon class loading, and 3 are tailored after makinString() invocation. Right?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Originally posted by Yaroslav Ashurin: Now I've got the picture... There are 5 String objects in the String pool, 2 of them are created upon class loading, and 3 are tailored after makinString() invocation. Right?
Yes. And sorry I missed the question about class and method earlier.
|
 |
Ravikanth kolli
Ranch Hand
Joined: Feb 10, 2008
Posts: 179
|
|
In the case of having 2 objects created at class loading. wont there be 6 objects totally? 1) String s = "Fred"; 2) "47" 3) Fred47 4) s = s.substring(2,5); 5) s = s.toUpperCase(); 6) s.toString(); please correct me if i am wrong.
|
-kolli
|
 |
Ravikanth kolli
Ranch Hand
Joined: Feb 10, 2008
Posts: 179
|
|
|
Or is it that s.toString() doesnot create a new string?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
Originally posted by Ravikanth kolli: Or is it that s.toString() doesnot create a new string?
Yes, String.toString() looks like public String toString() { return this; }
|
[Jess in Action][AskingGoodQuestions]
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[YA]: Now I've got the picture... There are 5 String objects in the String pool, 2 of them are created upon class loading, and 3 are tailored after makinString() invocation. Right? There are 5 String objects, but only the first two are in the String pool. The other three are plain String objects, not in any kind of pool. The string pool is only for (a) String literals and constant expressions, and (b) Strings where you've invoked the intern() method. [ April 25, 2008: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Ravikanth kolli
Ranch Hand
Joined: Feb 10, 2008
Posts: 179
|
|
Thanks a lot guys got it
|
 |
Yaroslav Ashurin
Greenhorn
Joined: Apr 25, 2008
Posts: 18
|
|
|
Thank you everybody for participating! Should I close the thread? What the forum's policy about that?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
You're welcome. I don't think you can close the thread; you simply leave it and it vanishes into history . . . until somebody searches for it.
|
 |
 |
|
|
subject: Question on String objects
|
|
|