Conversion from mutable to immutable class
When it comes to immutability of java, it’s important for us to know about data type of String. Each time we try to modify the string, it would create a new object with new value. When it comes to mutability of java, the sample data type for it would be such like array, and we can modify the elements on specific index. However, it is a more interesting topic in this article: How to create an immutable class by ourselves?
Here comes with a mutable class first
It is very easy for us to understand the function of the class above. The class would create a list and provide user with choices of adding and removing elements from the list.
Give me an Immutable Queue
Now, change the client interaction in main function to create an immutable version of class Queue. Let’s go back to the object String again. Each time we change the string, it would create a new object and refer to it. Therefore, what we need to do here is also create a new object and assign the value to it!
Therefore in the whole process, we take interaction with 4 IQ objects.