Home arrow static arrow Java Programming [Archive] - String constructors
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - String constructors
This topic has 6 replies on 1 page.

Posts:49
Registered: 6/23/04
String constructors  
Jun 23, 2004 2:45 PM



 
I have often seen in java examples and tutorials code like:
void method(String[] message) {    String s = new String("Msg: " + message);    ...

and I am wondering if the following is equivalent:
void method(String[] message) {    String s = "Msg: " + message;    ...

Because I have the suspicion that the first syntax first creates a string (to pass as a parameter) and then the String constructor creates and returns another one. Is that true or the compiler/JVM makes sure this double work is not propagated into actual bytecode?

Also, I am confused: what is the use of a constructor for String that takes a String as a parameter? For all the imagination that I can muster I cannot find a trickier way to waste memory, since either the parameter for the constructor is a literal string or a reference to one (and then I think that the constructor is creating an unnecessary duplicate) or the parameter is a result of some operation, like concatenation in the example above, and concatenation I suppose implicitly calls the String constructor, unless it creates strings out of thin air...

I'm learning Java as a Foreign Language (JFL) so please you language purists pardon my clumsiness. Maybe this question was posted, answered and flamed before...
 

Posts:6,750
Registered: 1/25/04
Re: String constructors  
Jun 23, 2004 3:11 PM (reply 1 of 6)



 
Also, I am confused: what is the use of a constructor
for String that takes a String as a parameter?

There is no use.

I'm learning Java as a Foreign Language (JFL) so
please you language purists pardon my clumsiness.
Maybe this question was posted, answered and flamed
before...

Many times. I recommend using Google to search these forums, using their "site:" feature. Very handy.
 

Posts:3,081
Registered: 2/15/99
Re: String constructors  
Jun 23, 2004 3:11 PM (reply 2 of 6)



 
Yes, the String(String) constructor is completely superfluous.

(String(String) affects synchronize() and the == operator, but anyone seriously using it for those purposes deserves a prime-time televised public flogging with a wet noodle).

The most useful purpose for String(String) is as a red flag: this code was written by a n00bie, tread carefully, here be dragons.
 

Posts:1,085
Registered: 4/14/03
Re: String constructors  
Jun 23, 2004 3:15 PM (reply 3 of 6)



 
The String constructor that takes a String parameter will always create a new String object. The compiler will never optimize that away. There are very, very few times when this will be useful, so generally it should be avoided.

So, you might wonder why it is even around. Well, one good reason is that a String can have a reference to a character array that is much larger than the content of the String itself. The new String(String) constructor will create a String with a backing array that is only as large as it needs to be. This isn't normally a problem, but if you notice that a lot of application memory is being eaten up by unnecessarily large character arrays, then using this constructor in a few appropriate spots may fix it. However, you should never optimize code until you have profiled your application and determined that the 'problem' you are trying to fix is what is actually causing problems.
 

Posts:11,200
Registered: 7/22/99
Re: String constructors  
Jun 23, 2004 3:17 PM (reply 4 of 6)



 
(String(String) affects synchronize() and the ==
operator, but anyone seriously using it for those
purposes deserves a prime-time televised public
flogging with a wet noodle).
Either that, or a very competent performance-aware programmer who is taking a small substring from a huge string and knows that the backing char array is shared.
 

Posts:3,081
Registered: 2/15/99
Re: String constructors  
Jun 23, 2004 3:20 PM (reply 5 of 6)



 
Uh, I forgot: there is one half-reasonable (though undocumented and thus unreliable) use for the String(String) constructor in some Java implementations: the substring() array sharing thing. But that's a bit unusual; if you need to know about that kinds of tricks, you know how to read the String.java source...
 

Posts:3,081
Registered: 2/15/99
Re: String constructors  
Jun 23, 2004 3:21 PM (reply 6 of 6)



 
...which is what jsalonen was referring to...
 
This topic has 6 replies on 1 page.