Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on String objects

 
Yaroslav Ashurin
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Yaroslav Ashurin
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you indeed, Campbell! You say "class is loaded". Did you mean "method"?
[ April 25, 2008: Message edited by: Yaroslav Ashurin ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yaroslav Ashurin
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or is it that s.toString() doesnot create a new string?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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; }
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys got it
 
Yaroslav Ashurin
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everybody for participating! Should I close the thread? What the forum's policy about that?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic