aspose file tools*
The moose likes Beginning Java and the fly likes Immutable Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Immutable Objects" Watch "Immutable Objects" New topic
Author

Immutable Objects

naro pad
Ranch Hand

Joined: May 07, 2008
Posts: 58

Hi guys,

I am facing one easy question and I want your help. I am trying to understand immutable objects and I have read that Strings belongs to that category, meaning that their values cannot be changed after they are created. What this mean? I have create the following code and it's change fine:


Do I missed something?

Thanks
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
When we say immutable, we mean the object cannot be changed. For instance, if I have the below code



Now we have created a String object with the value "Bacon is delicious". When we try to add " and nutritious!" to that object....well, we can't. Strings are immutable. So Java actually creates a whole new object, with the value "Bacon is delicious and nutritious!" and assigns it to test. The original String object containing "Bacon is delicious" still exists, we just don't have a variable pointing to it.*

*There was also a String object " and nutritious!" created, but String creation is a little off-topic.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Immutable means you can't change the values of the object. For instance, if you have an object with a field that you can modify either directly or through some method, then that object is called mutable.

However, if you have an object that never changes once its created, it's called immutable. String is such a class. Once you've created a String (remember that you create an instance of a String when you use a String literal), you can never change the value of that String.

Just take a String reference, and try calling any of its methods. You won't be able to change anything about the variable.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Keep in mind that 's' is just a variable that references an object. The variable is not the object itself.

The first object that 's' references is a String, "defaultValue." This object is immutable. It will always be "defaultValue."

But you can reassign the variable to point to another object. "newValue" is an entirely different String object (also immutable). When you assign 's' to point to this second String, the first String object, "defaultValue" remains unchanged. It's simply no longer referenced by 's'.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
naro pad
Ranch Hand

Joined: May 07, 2008
Posts: 58

I think I got it...

In my example when I reassign the String variable to 'newValue' a new reference value was created with the old value existing but without any reference to it.
So I have to be careful when creating immutable objects because of leaving them unused in the memory until garbage collector runs.

Thanks a lot
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

naro pad wrote:...In my example when I reassign the String variable to 'newValue' a new reference value was created with the old value existing but without any reference to it...

You got it!
naro pad wrote:... So I have to be careful when creating immutable objects because of leaving them unused in the memory until garbage collector runs...

Well, that's a more complex issue that goes beyond immutable objects (and is further complicated by the String pool). But you have the right idea, because unreferenced objects will contine to exist in memory until collected.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11421
    
  16

an object is like a house, and a variable is like a note card with the address of the house written down on it.

String s = "defaultValue";

creates a String object with a value of 'defaultValue'. it creates a String reference called 's' that has the address of the object written down on it.

s = "newValue";

creates a new String object with a value of 'newValue'. The old address stored in 's' is erased, and the new address is written on it. The old object with 'defauleValue' still exists somewhere. You simply don't have the address anymore so you can't find it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Immutable Objects