File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Immutable - Mutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Immutable - Mutable" Watch "Immutable - Mutable" New topic
Author

Immutable - Mutable

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Immutable - which does not change while Mutable - which can change

Is there any other difference ?


Regards,<br />Arnab
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That's it.


"I'm not back." - Bill Harding, Twister
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
You are absolutely right. String in Java are immutable, you can't change. When you do something like this:

String = "hi"+"bye";

There are actually three strings "hi", "bye" and "hibye" at SLP.

[That's not really true, but since this is the Beginner forum we will PRETEND it's true to keep things simple. Please see here for discussion of the more complex aspects. - Jim]

Not sure whether it is giving any additional information or not.
[ November 25, 2005: Message edited by: Jim Yingst ]
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
This is getting confusing.. My original question was about Immutable / Mutable. And as mentioned - Strings are immutable!

So if I have a piece of code



"s" has changed. Right ? So how are strings immutable ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
To Arnb, Tony and anyone else who was following this discussion - I have made a copy of the original thread, which is now here in Intermediate. In this thread I have deleted references to the special properties of String literals, so that hopefully Arnb can understand the more basic aspects first. Please see the thread in Intermediate for further discussion of the more complex aspects.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The variable s has changed - but the String object which s originally referred to has not changed. You can see this in the following code:

Both s and t are reference variables which initially point to a String object with content "hibye". You can change the variable s so that it points to a new and different String object, "new hibye", but that has no effect on the original String object, which is what t is still pointing to.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
"s" has changed. Right ? So how are strings immutable ?

Yes. The identifier 's' now points to the String "new hibye" instead of the String "hibye". The Strings haven't changed, but the reference variable 's' has changed. 's' is not a String, it is a pointer to a String. You have changed which String 's' points to.

If you stand outside your house and point to your house, then you point to your neighbor's house, you (the pointer) changed, but the houses didn't change.
[ November 25, 2005: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Arnb Sen:
This is getting confusing.. My original question was about Immutable / Mutable. And as mentioned - Strings are immutable!

So if I have a piece of code



"s" has changed. Right ? So how are strings immutable ?


I see this problem often, and it is typically the result of:
a) the lack of distinction between references and objects
b) the inability to efficiently talk about objects because they have no name
c) the loose definition of "immutable"

These points are related. In your case, "s" is a String reference that refers to some (unnamed) object. It is the object that is immutable, not the reference (since as you have observed, you can reassign it). One way to talk about the object is "the object that s refers to". You'll find that you cannot (within some specific bounds) change the state of that object. This digresses to the definition of "immutable", since as a client of the object, you never know (or care) about its state. What you might about, however, is the idempotence of its "contract" (its API). For example, given the String object "xyz", the charAt contractual operation (API method) is idempotent - that is, it will consistently return the same value upon consecutive calls.

It is generally accepted (either inadvertantly or not), that if a class' operations (methods) are all idempotent, that the type is "immutable". This holds for java.lang.String.

Also, see if this helps
http://jqa.tmorris.net/GetQAndA.action?qids=75&showAnswers=true


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Immutable - Mutable