Sunday, 10 September 2017

What is an immutable class?


Immutable class is a class which once created, it’s contents can not be changed. Immutable objects are the objects whose state can not be changed once constructed. e.g. String class .

Ques: How to create an immutable class?



To create an immutable class following steps should be followed:


1.Create a final class so that it cannot be inherited.
2.Make the member variable private so that fields cannot be accessed outside class. 
3. Make the member variable final so that fields can be assigned only once. 
4.Set the values of properties using constructor only.
5.Provide only getter method().
6.Do not provide any setter() for these filed.


package com.tutorial4u;

final class Student {
  private final String name;
  private final int regNo;
  public Student(String name, int regNo) {
         this.name = name;
         this.regNo = regNo;
  }
  public String getName() {
         return name;
  }
  public int getRegNo() {
         return regNo;
  }
}


Ques: Which classes in java are immutable?


All wrapper classes in java.lang are immutable – String, Integer, Boolean, Character, Byte, Short, Long, Float, Double, BigDecimal, BigInteger

Ques: What are the advantages of immutability?

• Immutable objects are automatically thread-safe, the overhead caused due to use of synchronization is avoided.

• Once created the state of the immutable object can not be changed so there is no possibility of them getting into an inconsistent state.

• The references to the immutable objects can be easily shared or cached without having to copy or clone them as there state can not be changed ever after construction.

• The best use of the immutable objects is as the keys of a map. .

Ques: What is Advantages of using Immutable class ?

• Thread safe - Immutable classes are thread safe, they will never create race condition.

• Key in HashMap - Immutable classes are can be used as key in Map (HashMap etc.)

• HashCode is cached - JVM caches the HashCode of Immutable classes used in application. JVM need not to calculate hashcode again. Hence, performance of application is improved significantly.

• If Immutable class throws Exception - If Immutable class throws Exception, they are never left in undesirable state.

Ques: Benefits of Immutable Classes in Java.



As I said earlier Immutable classes offers several benefits, here are few to mention:

1) Immutable objects are by default thread safe, can be shared without synchronization in concurrent environment.

2) Immutable object simplifies development, because its easier to share between multiple threads without external synchronization.

3) Immutable object boost performance of Java application by reducing synchronization in code.

4) Another important benefit of Immutable objects is reusability, you can cache Immutable object and reuse them, much like String literals and Integers. You can use static factory methods to provide methods like valueOf(), which can return an existing Immutable object from cache, instead of creating a new one. .

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...