This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new String(text);" Watch "new String(text);" New topic

new String(text);

frantisek nejedly
Ranch Hand

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

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

Thank you

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183


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.
Don't get me started about those stupid light bulbs.
subject: new String(text);
Similar Threads
File Open/Save Operations in Swing Editor
it really a new world.......
Error in Package
Nested code in If statement
Application that implements WindowListener