5

I read a chapter of Effective Java today. The chapter is about minimizing mutability of objects. I pretty understand why immutable objects are good, such as thread-safe, simplicity, etc. My question is since immutable objects have so many advantages, are there any cases where mutable objects are preferred?

0

3 Answers 3

12

You need mutable classes to deal with situations when transforming a class from state A to state Z would produce a lot of intermediate objects, which you would rather not spend time creating.

One classic example is concatenating an array of strings. Let's say that you need to concatenate 100 strings, and produce a single result. Without a mutable string, you would need to produce 98 intermediate objects that would become eligible for garbage collection almost immediately. This is rather wasteful on the CPU, making a mutable object the right solution.

There are other situations when mutable objects are desirable. For example passing mutable objects into methods lets you collect multiple results without jumping through too many syntactic hoops. Another example is sorting and filtering: of course, you could make a method that takes the original collection, and returns a sorted one, but that would become extremely wasteful for larger collections.

2

Immutable classes may waste memory and garbage collector time as changing of data will generate wasted instances. Functional programming methods, however, are pretty effective with immutable classes.

Functional methods can be used to modify data and then provide immutable class to the developer. This way mutable classes are abstracted away from developers. Functionality comes with other good things such as lazy loading and behind the scenes optimization of these abstract calls.

Java 8 streams API can be used to provide functional way of handling data. You may also want to try Scala which is kind of a functional variant of Java.

That said, it may be best to leave the most complicated and performance critical tasks to more conventional programming practices. With carefully planned architecture and proper synchronization mutable objects will work very well. High amount of traffic gets garbage collector pretty busy when using only immutable objects.

There's always a limit to amount of functionality that functional programming methods can reasonably abstract away. Functional programming seems to work great in front-end development where architectural complexity isn't usually very high.

1
  • Scala has significantly different syntax. Calling it "a kind of a functional variant of Java" may be misleading...
    – pkalinow
    Commented Mar 6, 2015 at 9:07
1

This is because of nature of mutable states/attributes of objects. For example a Person's age will change, an Employee salary will change, Television's volume/channel/brightness can change etc over a period of time.

There would be tons of example where an Object's states can change.

3
  • 1
    You can change the state of a (mutable) object or you can make a new (immutable) object whenever you need a new state. Others have given examples in which "make a new object" is especially undesirable, but the mere fact that you might need more than one state during execution is not such an example. In some threaded applications you might have things whose real-life state changes constantly but you want to capture those states in immutable objects.
    – David K
    Commented May 12, 2014 at 19:18
  • Well said @DavidK, but there are reasons and scenarios where we do use mutable objects due to natural choice of state change instead of creating new immutable objects always. This choice comes (mostly) while designing applications where a long-lived object carries various states!
    – harsh
    Commented May 12, 2014 at 19:36
  • "Some applications" is not all applications, of course! There is usually a cost to creating a new object every time you have a new state. Is that cost justified by other design considerations? If not, you make the object mutable. But the whole point is to understand what those other design considerations are and when they are not sufficient.
    – David K
    Commented May 12, 2014 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.