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
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 ]
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..."