Sub topic 2
Immutable Values: Every Change Returns a New Object
In java.time, a date or time object never changes after you create it. This is the most important rule in the whole API. Once you understand it, most java.time code reads clearly.
What "immutable" means
An immutable object is an object whose values cannot change after it is built. A LocalDate that holds March 18, 2014 will hold March 18, 2014 for as long as it exists.
So what happens when you call a method like plusDays? It leaves the old object alone. It builds a new object with the new value and returns it. To keep that new value, you must save it in a variable.
Why it matters
The old java.util.Date class was mutable. A mutable object is one whose values can be changed in place. With Date, one part of a program could change a date and break another part that shared the same object.
Immutable objects remove that risk:
- Safe sharing. Many parts of a program can use the same object, and none of them can change it for the others.
- Thread safety. A thread is a path of code that runs at the same time as other code. Several threads can read an immutable object at once without errors.
- Fewer surprises. A value you checked a moment ago is still the same value.
The common mistake
Beginners often call a method and forget to keep its result. Look at this program:
import java.time.LocalDate;
public class Immutable {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2024, 1, 31);
date.plusDays(1); // result discarded, date is unchanged
System.out.println(date); // 2024-01-31
LocalDate next = date.plusDays(1);
System.out.println(next); // 2024-02-01
}
}The first call to plusDays(1) builds a new date, but nothing saves it. Java throws it away. The variable date still holds January 31. The second call saves the result in next, and that variable holds February 1.
Chaining calls
Each method returns a new object, so you can call another method on that result right away. This is called chaining:
import java.time.LocalDate;
public class Chaining {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2024, 1, 1);
LocalDate later = start.plusMonths(2).plusDays(5);
System.out.println(start); // 2024-01-01
System.out.println(later); // 2024-03-06
}
}Every step in the chain makes a fresh object. start stays the same.
Methods that follow this rule
| Method style | Example | What it returns |
|---|---|---|
| `plus...` | `plusWeeks(2)` | A new object moved forward |
| `minus...` | `minusHours(3)` | A new object moved back |
| `with...` | `withYear(2030)` | A new object with one field replaced |
Exercise
What does this code print? LocalDate d = LocalDate.of(2024, 2, 28); d.plusDays(1); d = d.plusDays(2); System.out.println(d);
Show answer
It prints 2024-03-01. The first plusDays(1) result is thrown away, so d is still February 28. The next line assigns the result back to d. 2024 is a leap year, so two days after February 28 is March 1.
Exercise
Why can two threads safely read the same LocalDate object at the same time?
Show answer
LocalDate is immutable. Neither thread can change the object, so both always see the same value.