Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Immutable - Mutable

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Immutable - which does not change while Mutable - which can change

Is there any other difference ?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's it.
 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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 ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic