• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings are immutable ? is it true or not  RSS feed

 
tue march
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because toUpperCase returns a new String:

String test = "hello";
String upper = test.toUpperCase();

-Henrik
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!