This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Strings are immutable ? is it true or not Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Strings are immutable ? is it true or not" Watch "Strings are immutable ? is it true or not" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings are immutable ? is it true or not
 
Similar Threads
Immutable String ??
Immutable Strings
Strings Immutable ?
immutable
Question on immutability