Invariant objects are objects whose externally visible state cannot be changed after instantiation. The String, Integer, and BigDecimal classes in the Java class library are examples of immutable objects-they represent a single value that cannot be changed during the lifetime of an object.
The strength of invariance
If you use invariant classes correctly, they can greatly simplify programming. Because they can only be in one state, they will never fall into an inconsistent state as long as they are constructed correctly. You can freely share and cache references to the invariant objects without duplicating or cloning them, and you can cache their fields or the results of their methods without worrying about whether the value will become invalid or inconsistent with the other states of the object. Invariant classes usually produce the best mapping keys. Moreover, they are inherently thread-safe, so there is no need to synchronize access to them between threads.
Free cache
Because the values of unchanging objects are not dangerous to change, they can be freely cached and can be sure that subsequent references will still reference the same value. Similarly, because their attributes cannot be changed, you can cache their fields and the results of their methods.
If the object is mutable, it must be noticed when the reference to it is stored. Consider the code in Listing 1, which arranges two tasks performed by the scheduler. The goal is to start the first task now and start the second task one day.
Listing 1. Potential problems with a mutable Date object
Date d = new Date();
Scheduler.scheduleTask(task1, d);
d.setTime(d.getTime() + ONE_DAY);
scheduler.scheduleTask(task2, d);
Because date is variable, the ScheduleTask method must be careful to copy the date parameter (possibly through clone ()) to its internal data structure with a precautionary measure. Otherwise, Task1 and Task2 may all be executed tomorrow, which is not what is expected. Worse, the internal data structures used by the Task Scheduler become corruption. When writing a method like ScheduleTask (), it is extremely easy to forget to use precautions to copy date parameters. If you forget to do this, you create an error that is difficult to catch, and the error does not appear immediately, and it takes a long time for it to be captured when it is exposed. This type of error cannot occur with the invariant Date class.
Inherently thread-safe
Most thread-safety problems occur when multiple threads are trying to modify the state of an object (write-write conflict) concurrently, or when a line one thread attempts to access the state of an object while another thread is modifying it (read-write conflict). To prevent such conflicts, you must synchronize access to shared objects so that other threads cannot access them when the objects are in an inconsistent state. It can be difficult to do this correctly, requiring a large number of documents to ensure that the program is properly scaled, and may have negative performance consequences. As long as invariant objects are constructed correctly (which means that object references are not converted from constructors), they are exempt from the requirement for synchronous access because their state cannot be changed so that there is no write-write conflict or read-write conflict.
The freedom to share references to immutable objects without synchronization can greatly simplify the process of writing concurrent programs and reduce the number of potential concurrent errors that may exist for a program.
It's safe to run malicious code.
Methods that treat objects as arguments should not change the state of those objects unless the document explicitly states that it can be done, or the methods actually have ownership of the object. When we pass an object to the normal method, we usually do not want the object to be returned when it has been changed. However, this is exactly what happens when you use a Mutable object. If you pass Java.awt.Point to a method such as component.setlocation (), it does not prevent setlocation from modifying the location of the point that we passed in, nor does it prevent setlocation from storing a reference to it and later Change it in another method. (Component, of course, does not do this because it is not reckless, but not all classes are so polite.) Now, the state of point has changed without our knowledge, and the result is potentially dangerous--we still think it's in the same position when it's actually in another position. However, if point is invariant, then this malicious code cannot modify our program state in such a messy and dangerous way.