------------
In java , immutable means , if we assign any value to string , means after creating an object that we can not re-assign a value to that particular staring object.
Example:
- public class ImMutableString {
- final String n;
- ImMutableString(String n) {
- this.n = n;
- }
- public String getN() {
- return n;
- }
- // this setter can modify the name
- public void setN(String n) {
- this.n = n;
- }
- public static void main(String[] args) {
- ImMutableString obj = new ImMutableString("Amit");
- System.out.println(obj.getN());
- // update the name, this object is mutable
- obj.setName("Rahul");
- System.out.println(obj.getN());
- }
- }
when we are going for print this value, we will get only output as a Amit not Rahul,
This is a concept of immutable.
mutable
--------------
In java , mutable means , if we assign any value to StringBuffer or StringBuilder , means after creating an object that we can re-assign a value to that particular staring object.
Example:
- public class MutableString {
- private String n;
- MutableString(String n) {
- this.n = n;
- }
- public String getN() {
- return n;
- }
- // this setter can modify the name
- public void setN(String n) {
- this.n = n;
- }
- public static void main(String[] args) {
- MutableString obj = new MutableString("Amit");
- System.out.println(obj.getN());
- // update the name, this object is mutable
- obj.setName("Rahul");
- System.out.println(obj.getN());
- }
- }
when we are going for print this value, we will get only output as a Amit and Rahul both,
This is a concept of mutable.