• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new String(text);

 
frantisek nejedly
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please what is the difference between

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

Thank you
T.

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
frantisek nejedly
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought so, now I am sure.
Thank you very much Ernest.
T.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic