Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in String

 
sekhar variam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
I have a doubt in String..This is regarding a type of question I have seen several times in mock tests...
Consider the following code
String s = "abcd";
String s1 = s;
How many Stirng objects are created...I am a bit confused regarding this.
Thanks
Sekhar
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one object is created. The fact of assigning a reference to a String object to another String reference does not imply that a new String object is created, only that s and s1 will reference the same String object.
[ December 05, 2002: Message edited by: Valentin Crettaz ]
 
Jon Huhtala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sekhar,
Only one String object is created. The compiler generates a String object in the literal pool for every literal string, such as "abcd". The variables s and s1 are merely references to the same String object. If, however, you had coded:
String s1 = new String(s);
A new String object would have been instantiated.
For more information, see
java-help.com
[ December 05, 2002: Message edited by: Jon Huhtala ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic