1

I had read somewhere in book that Immutable String can be implemented more efficiently than the changeable one.

How Immutable String are more efficient compared to Changeable?

I mean what is the implementing difference between Immutable and changeable string that makes Immutable string more efficient...??

4
  • You have some inconsistencies in your question/title that you might want to clear up... "Mutable" means the same thing as "changeable" Commented May 24, 2014 at 5:19
  • Which book, and what was the context? "Implemented more efficiently" can mean many things. It's absolutely easier to implement a core class like String in an immutable way: requires less code, avoid headaches with data races in multi-threaded programming, reliable security checks are easier to implement, etc. Or do you mean more efficient to execute ? Commented May 24, 2014 at 6:11
  • The Complete Reference 7th edition page no: 359 Commented May 24, 2014 at 6:42
  • yes i am asking about more efficient to execute. Commented May 24, 2014 at 6:43

4 Answers 4

5

Read here.

Both mutable and immutable objects have their own uses, pros and cons.

Immutable objects do indeed make life simpler in many cases. They are especially applicable for value types, where objects don't have an identity so they can be easily replaced. And they can make concurrent programming way safer and cleaner (most of the notoriously hard to find concurrency bugs are ultimately caused by mutable state shared between threads). However, for large and/or complex objects, creating a new copy of the object for every single change can be very costly and/or tedious. And for objects with a distinct identity, changing an existing objects is much more simple and intuitive than creating a new, modified copy of it.

Think about a game character. In games, speed is top priority, so representing your game characters with mutable objects will most likely make your game run significantly faster than an alternative implementation where a new copy of the game character is spawned for every little change.

Moreover, our perception of the real world is inevitably based on mutable objects. When you fill up your car with fuel at the gas station, you perceive it as the same object all along (i.e. its identity is maintained while its state is changing) - not as if the old car with an empty tank got replaced with consecutive new car instances having their tank gradually more and more full. So whenever we are modeling some real-world domain in a program, it is usually more straightforward and easier to implement the domain model using mutable objects to represent real-world entities.

Sign up to request clarification or add additional context in comments.

Comments

2

I won't say immutable objects are more efficient; it is more preferable even though it is not as efficient as mutable objects

pros

  • Easily maintainable
  • Thread safe

Easily maintainable means, once an immutable object is created its state can't be changed (by mistake) when you pass it around multiple business layer; therefore, it mitigates the number of defects related to incorrect state change

You can also share the immutable object in a multi-threaded environment without worrying about data conflicts as the state of the object can't be changed.

e.g.: BigDecimal

Cons

  • Performance impact

As you can imagine, a new object is required for every state/data. You can improve the performance a bit by implementing caching mechanism for popular values.

e.g.: BigDecimal.ZERO

Being said that, you should try to create your user domain object as immutable as possible (by providing limited setter methods) because the advantage of easily maintaining an immutable object outnumbers the performance of mutable object.

Comments

1

String chaching is possible with immutable strings which can speed up performance. On the flip side it's much LESS efficient to use immutable string if you're going to concatenate lots of large strings together.

Comments

0

Strings immutability makes it possible to share them, this is more efficient memory-wise.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.