File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes new String(text); 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 "new String(text);" Watch "new String(text);" New topic
Author

new String(text);

frantisek nejedly
Ranch Hand

Joined: Sep 09, 2008
Posts: 42
Please what is the difference between

textA = text;
and
textA = new String(text);

Thank you
T.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

The first one makes the variable point to an existing String; the second creates a new String. There is almost never (and by almost never I mean only in one or two very advanced cases that you're unlikely to ever run into) any reason to use the String(String) constructor. Always use your first version, not the second.


[Jess in Action][AskingGoodQuestions]
frantisek nejedly
Ranch Hand

Joined: Sep 09, 2008
Posts: 42
I thought so, now I am sure.
Thank you very much Ernest.
T.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new String(text);