• 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why it is better to make objects immutable whenever possible?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I'm new in java.

I know what is immutable objects and mutable objects, but I am confusing about why it is better to make objects immutable whenever possible?

And is there have any time to use mutalbe objects better?

Thanks.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advantage of immutable objects is that you can share them--pass them to methods, return them from methods, etc.--without having to worry that one of the pieces of code that touches them will change them.




After calling method foo, we KNOW that the String object still holds "abc", because Strings are immutable. We don't know if the Date and List object still hold the state (the contents) that we gave them. We have no idea what they hold, because they are mutable.
 
Xiaoguang Huang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeff, I'm very appriciate of your answer.
 
Marshal
Posts: 74602
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can also gain access to immutable objects from several threads simultaneously without any synchronisation problems. Yes, immutable objects are inherently thread-safe.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And no duplicate objects are created if one already exists. saves memory.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsha Smith wrote:And no duplicate objects are created if one already exists. saves memory.


That only applies to string literals, interned Strings and certain values of primitive wrapper classes. Other than that there is nothing to stop you creating a duplicate of an immutable object.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic