aspose file tools*
The moose likes Beginning Java and the fly likes Use of default String constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use of default String constructor" Watch "Use of default String constructor" New topic
Author

Use of default String constructor

Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
Can any body tell me what is the use of default constructor of String class,
I know we can create empty string from it but where to use that empty string...


Thanks

Living is easy with your eyes closed.....
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The API says :
Initializes a newly created String object so that it represents an empty character sequence. Note that use of this constructor is unnecessary since Strings are immutable.


[My Blog]
All roads lead to JavaRanch
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
Is it really necessary to initialize it.....we can initialize it wit null also...

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

If you make a String, you want to assign a value to it once. Remember that it's immutable. If you need to use null, String str = null is enough.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Inder Kumar Rathore wrote:Is it really necessary to initialize it.....we can initialize it wit null also...

That's not the same. new String() is the same as "", not null.

But I agree that the default constructor is never really needed from code, just like the constructor that takes another String. Both are remainders of old times and may still be used in reflection, but other than that they should be shunned.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: Use of default String constructor