Monday, October 12, 2015

Understanding Mutability and Immutability in Java


Mutable Class :

Class which can be changed is mutable. Most of Concrete classes are Mutable only.

Immutable Class :

Which can not be changed is immutable. Standard Example : String Class.

Ques. Why String is Immutable ?

String country ="India";
 Here, India is stored in String Constant Pool.

Again, 
String countryName ="India";
Here, countryName is also referring to same Memory location where India is stored in String Constant pool.

String countryName = countryName + "My Country";



Ques : How can we make our custom class which is Immutable ? (Standard Interview Question)

  


No comments:

Post a Comment