Java Immutable Explained. Immutable Objects With Examples.

What This Article About

Immutability is a broad concept and has many angles. Immutability is a set of practices that potentially might reduce the number of bugs in your application. As a consequence, this subject invokes confusion. Here, we review it from a different perspective. I'll explain the most important immutability aspects in clear language.

Mutability Problems Examples

Before we start, let's review two examples. They are pretty artificial but explain potential problems with mutable code:

Java SimpleDateFormat Is Not Simple

Formatting and parsing dates is a daily (painful) task. Every day, it gives us another headache.

A common way to format and parse dates in Java is using SimpleDateFormat. Here is a common class we can use.