aspose file tools*
The moose likes Beginning Java and the fly likes new String() in constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new String() in constructor" Watch "new String() in constructor" New topic
Author

new String() in constructor

Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
If I declare an instance variable.. String something and inside the constructor of this same class I have:

className(String passedVariable)
{

something = new String();

something = passedVariable'

}

What will happen? Will the new String() object be put on garbage collection? Or will it be used to store the value of passedVariable.. If I didn't create a new String() object.. would java create one automatically and then store it.. I get confused on this.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The new String() part is completely pointless. It creates an empty String object that you never use and that immediately becomes eligible for GC because you reassign the reference variable.

When you do var = something for a reference type variable (anything other than the primitives--byte, char, short, int, long, float, double, boolean) it never copies an object. It just copies a reference value.

Regardless of whether you do new String() or not, the only thing something = passedVariable; does is to copy the reference value from passedVariable into something.
Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Thanks Jeff.

I was helping someone out with their code, and I knew this wasn't correct and this was the way they were doing it. I wasn't 100% if how I understood it was correct.
So also, if something = "some name"; (after it was already assigned to new String()) It would replace the new String(); with another string object set to "some name" right?
So there really is no point of creating new String() ever unless you are creating an array of strings correct?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Joseph Cho wrote:Thanks Jeff.


You're welcome.

So also, if something = "some name"; (after it was already assigned to new String()) It would replace the new String(); with another string object set to "some name" right?
So there really is no point of creating new String() ever unless you are creating an array of strings correct?



Variables hold references, not objects, so it doesn't replace any object; it just replaces the reference. But yes, you're correct, there's no point in doing new String() there. I don't know that there's ever any point in doing new String() anywhere. If you want an empty String, just use "", but even that is usually not necessary. As for arrays, if you really need the array to start off with a bunch of empty Strings rather than nulls, you could set each element to "'" (or using new String(), but, again, that's pointless), but it's more common to just leave them null until you have a "real" value to give them. It depends on your specific requirements though.

[EDIT] Or were you maybe thinking of new String[length]? That creates an array of String references, each initialized to null. It creates String[] object, but no String objects. Doing new String() on the other hand, creates a String object with no characters.
Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Okay thanks for clearing that up.. and yes I was thinking String[length]..

Thanks again!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Joseph Cho wrote:So why is it that you do not need to declare a new String() but you would have to say... declare a new JPanel to a instance of a jpanel?


String is a special case. Since it's so common, to avoid some tedious typing and cluttered code, the language allows us to use String literals. When we write the compiler has been written to honor the language rule that "abc" is interpreted as a reference to a String object. The appropriate character sequence is put into the .class file, and when the class is loaded, the JVM creates a new String object in the String pool if there's not already an equivalent one there. It may or may not use new String(...) at that point.

However, that doesn't mean that new String(...) is never used. There are probably a lot of places in the core API--inside toString() methods, in StringBuffer and StrinBuilder, and in I/O classes that create Strings from what they're reading. It's just that 1) Most of the cases where we need to use a String's constructor are already handled by lower level core API classes, and 2) Even then, they rarely if ever have a reason to use the no-arg new String() version to create an empty String.

Other examples of literals:


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: new String() in constructor