• Post Reply Bookmark Topic Watch Topic
  • New Topic

String creation  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is a new String object created in Java and when is an existing one used ?
To be precise, I understand that the declarations...
String str1 = "somestring";
String str2 = "somestring";
Leads to the creation of only one string object, as String literals are optimised.
However doing the following...
String str3 = new String(str1);
Creates a second object based on the the string passed to it (I think )
What about the case of a function which returns a String object ? If we had...
String str4 = ("domedtring").replace(d,s);
// should evaluate to "somestring"
Does this create yet another new String object or does it simply make str4 an alias to the existing "somestring" object ?
Thanks - Neil.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
String literals (i.e., strings that appear literally in Java source between double quotes) are shared, as you note. Every instance of "FOO" refers to a single String object, held internally in a special pool by the JVM.
Every String created via "new" is new and is distinct from every other string, including those containing the same characters.
To determine if a method returns a new String or not, you'd have to see the source. A method might sometimes return one of its arguments unchanged, and other times return a new one.
There's a method "intern()" which gives you access to the pool of unique string literals. If you call intern() on a String, and a String with that value was in the pool, then the one from the pool will be returned, so that
new String("FOO").intern() == "FOO"
is always true. If the object wasn't already in the pool, it's added.
In your example, replace() returns a new, unique string. It could if it wanted to, call intern() on its new string and thereby return the literal object -- but such a lookup would be an unnecessary computational expense, so it doesn't (also, since most results wouldn't be equal to literals, doing this would make the pool grow unnecessarily.)
OK?
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, I've got to remember to refresh a thread before replying...
However doing the following...
String str3 = new String(str1);
Creates a second object based on the the string passed to it (I think )

Yes. If you see a new String() constructor, that definitely is creating a new instance.
What about the case of a function which returns a String object ?
That really depends on the method; depending how the method is written, it could create a new instance, or not. Sometimes the API comments tell you which is the case; sometimes they don't. If they don't say, you can look at the source code, but the implemenentation may change in a futre release (or a pst one) so you should avoid making assumptions about other implementations.
If we had...
String str4 = ("domedtring").replace(d,s);
// should evaluate to "somestring"
Does this create yet another new String object or does it simply make str4 an alias to the existing "somestring" object ?

The API for this method says:
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

So in this case, a new String is created. You can also find confirmation in the source code, at least for one JDK version. I'm using J2SDK 1.4.2 - String.java line 1564 shows the "new String()" constructor that makes a new String.
Methods which return String values by creating new Strings are pretty common, but many other methods don't create new Strings. The most common example is that most methods named something like getXXX() probably just return a reference to an object that's already in memory. But not always. Really, there is no one answer to this question. You just have to look at the API for each method. If it doesn't say, then you have no guarantee, but you can look at source code to find out what one implementation does.
Hope that helps...
[ October 26, 2003: Message edited by: Jim Yingst ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!