Skip to main content

Posts

Showing posts with the label Immutable

How to Make a Java Class Immutable?

Immutability must be familiar to every one when we talk about String & StringBuffer classes in java. Strings are considered immutable because the values contained in the reference variable cannot be changed. Whereas String Buffer is considered mutable because the value in a string buffer can be changed (i.e. mutable). However I always thought how to make our user defined classes as immutable though I am unaware as to why any one would need this. The reason perhaps might be clear once we have a look at the code. Now in order to make a class immutable we must restrict changing the state of the class object by any means. This in turn means avoiding an assignment to a variable. We can achieve this through a final modifier. To further restrict the access we can use a private access modifier. Above do not provide any method where we modify the instance variables.

What are mutable objects and immutable objects?

An Immutable object is a kind of object whose state cannot be modified after it is created. This is as opposed to a mutable object, which can be modified after it is created.   In Java, objects are referred by references. If an object is known to be immutable, the object reference can be shared. For example, Boolean , Byte , Character , Double , Float , Integer , Long , Short , and String are immutable classes in Java, but the class StringBuffer is a mutable object in addition to the immutable String .   class Program { public static void main(String[] args) { String str = "HELLO"; System.out.println(str); str.toLower(); System.out.println(str); } }

What Is Immutable Object?

Immutable object: Immutable objects whose state (i.e. the object's data) does not change once it is instantiated (i.e. it becomes a read-only object after instantiation). Immutable classes are ideal for representing numbers (e.g. java.lang.Integer, java.lang.Float, java.lang.BigDecimal etc are immutable objects), enumeratedtypes, colors (e.g. java.awt.Color is an immutable object), short lived objects like events, messages etc. Benefits of immutable objects • Immutable classes can greatly simplify programming by freely allowing you to cache and share the references to the immutable objects without having to defensively copy them or without having to worry about their values becoming stale or corrupted.

Why String has been made immutable in Java?

Though, performance is also a reason (assuming you are already aware of the internal String pool maintained for making sure that the same String object is used more than once without having to create/re-claim it those many times), but the main reason why String has been made immutable in Java is 'Security'. Surprised? Let's understand why. Suppose you need to open a secure file which requires the users to authenticate themselves. Let's say there are two users named 'user1' and 'user2' and they have their own password files 'password1' and 'password2', respectively. Obviously 'user2' should not have access to 'password1' file. As we know the filenames in Java are specified by using Strings. Even if you create a 'File' object, you pass the name of the file as a String only and that String is maintained inside the File object as one of its members.