1

why are there some mutable objects in java? Why can everything just be immutable? Are there any disadvantages?

Comments
  • 0
    Java wasn't designed with immutability in mind. So, all objects are mutable by default, unless you work around by making them not mutable

    So, there is nothing to reason about their adv/disadvantages
  • 2
    Performance mostly. You really don't want to be allocating new objects and copy the old data over on each little change
  • 3
    Immutability is great for detecting bugs, but at a very heavy performance cost. It’s an expensive safety net.
  • 0
    I think it’s easy to overestimate the performance discredits of immutability. Optimising compilers can reduce the load incurred from having to recreate large objects on minor changes by reallocating unchanged memory. Also you often will use more granular object composition when working with immutable structures because you can’t configure a monolith for every use case.
  • 0
    I'll take the high road and reflect the question back at ya:

    Why should everything (!) be immutable?

    Just think about it, go crazy for a bit and try to invent some scenarios.

    Immutability is one of many nice concepts in programming, but it doesn't fit everything.
Add Comment