• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Intern  RSS feed

 
SenthilKumar Ponnuswamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between the following two statements:

String str1 = new String("Sen");

String str2 = new String("Kumar").intern();
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I presume you are asking this as a question, rather than a recommendation, because you don't usually use the copy constructor for String.

To the API! If you need more explanation please ask again.

If there already is a String containing "Sen" the first statement will create a second String containing "Sen".

If there already is a String containing "Kumar" the second statement will create a new String, then replace it by the pre-existing String "Kumar."
***************************************************************************
Actually there already is a String "Kumar". You have written it in the code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!