If you look at String and StringBuilder implementations, you can clearly see how they’ve employed immutability. Check out the screenshots I’ve taken below.

Image for post
Image for post
Notice how they’ve made the storage ‘final’. That’s what causes the String class to become immutable
Image for post
Image for post
StringBuilder extends AbstractStringBuilder Class. Notice how the storage is not final. It is extendable.
Image for post
Image for post
When you append to a StringBuilder, the array is expanded in size in a predefined amount. They do this to chop-off the amortized time to add one additional character

Software Engineer @ CodeGen International, CSE Graduate @ University of Moratuwa

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store