| Author |
Doubt in String
|
sekhar variam
Greenhorn
Joined: Nov 09, 2001
Posts: 15
|
|
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
|
SCJP4(96%)
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
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 ]
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Jon Huhtala
Greenhorn
Joined: Apr 10, 2002
Posts: 17
|
|
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 ]
|
- Jon Huhtala, SCPJ2<p> "Nothing worthwhile is ever easy..."
|
 |
 |
|
|
subject: Doubt in String
|
|
|