• Post Reply Bookmark Topic Watch Topic
  • New Topic

what actually?  RSS feed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I heard & read so many times, that Stribuffer is Mutable & String is Immutable. what Mutable & Immutable actually means? please explain in the programmming perspective.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a practical definition, it means that none of the methods of String will change any observable property of that String, so that all Strings containing the same characters are always interchangeable; whereas many methods of StringBuffer will change the state of the StringBuffer, so that two StringBuffers that are the same at one moment may be, after some methods are called, no longer the same.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest going to the book store and check out Head First Java 2ed in Appendix B page# 661 & 669. It gives a good description of immutability.

From the looks of it (I'm a beginner so I may not understand this completely!) it looks like Strings and Wrappers are immutable.
Immutable meaning that the string object cannot be changed once created. If you change the String what happens is a new String object is created and the reference then points to that new String object. However the other String object sits in what is called the 'string pool' it isn't destroyed by the garbage collector.

So for example:



What happens is:

-First a string reference Called "myString" is created and linked to a new string object "A".

-Then a new String object "BC" is created and referenced to by "myString."

-Then, since the first String object "A" wasn't destroyed, when you tell the myString reference to reference an object "A" it links back to the original string object "A" instead of creating a new "A" string object.

It seems like they did this to save memory but if not used correctly you can actually waste lots of memory.

That's my understanding of it...

-Mike

[ September 21, 2006: Message edited by: Michael Farinha ]
[ September 21, 2006: Message edited by: Michael Farinha ]
 
Suhas Madap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
a small doubt, how a String object will get created without using the keyword "new"?
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is a shortcut provided by the compiler. You might imagine it is equivalent to

Reality is a bit trickier as the first form makes s refer to an object in a string constant pool and the second makes s refer to a new object that has the same content as the one in the pool.

So the compiler gives String a few exceptions to all the normal rules about object creation. The designers felt this inconsistency - and the thousands and thousands of times new Java students have to ask about it - were made worthwhile by performance optimizations.
 
Michael Farinha
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Stan,

Since I'm still new to this, is my explination about the string pool fairly accurate?
I just want to make sure I understand this concept.

Thanks!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think you're good there. Look at the doc for String.intern() and the link into the Java Language Spec from there for more. This isn't something I've had to worry about, ever. I'm having a hard time imagining a situation where I'd care about two strings being ==
 
Suhas Madap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
thank you very much for the very crystal clear explanation. thanks a lot!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!