| Author |
Strings are immutable ? is it true or not
|
tue march
Greenhorn
Joined: Mar 09, 2005
Posts: 16
|
|
Hello ppl, I know that Strings are fixed length, immutable right. consider the following example and explain how it achieves it. there is string called String s = "hello"; ok when u apply toUpperCase() to the above string it gets changed to "HELLO". THEN how are you saying it is not mutable. explain please. Thanks, good guy
|
 |
Henrik Engert
Ranch Hand
Joined: Apr 26, 2005
Posts: 68
|
|
Because toUpperCase returns a new String: String test = "hello"; String upper = test.toUpperCase(); -Henrik
|
SCJP 5.0<br />SCWCD
|
 |
Jan Groth
Ranch Hand
Joined: Feb 03, 2004
Posts: 456
|
|
in addition to Hendriks post: After that opration, you'll have two objects in the heap. "HELLO", referenced by s. "hello", not referenced by any variable, ready to be garbage collected. Greets, Jan
|
 |
 |
|
|
subject: Strings are immutable ? is it true or not
|
|
|