In java, there are two types of objects
1.
Immutable object
2.
Mutable object
Q) What is the difference between
the immutable object and mutable object?
Ans):-
Immutable object is a java object. It will not allow
modifications on its content, if we are trying to perform operations with
immutable object content then data is allowed for the operations, but the
resultant data will not be stored in original object, the resultant data will
be stored by creating new object
E x:-
All String class objects and all wrapper classes object are
immutable objects.
Mutable object is a java object, it able to allow
modifications directly on its content.
Ex:-
By default, all java objects are mutable objects.
Ex:-
StringBuffer object is mutable object.
Ex code :-
class Test
{
public
static void main(String[] args)
{
String
str1=new String("Sai");
String
str2=str1.concat(" Software");
String
str3=str2.concat(" Solution");
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println();
StringBuffer
sb1=new StringBuffer("Sai");
StringBuffer
sb2=sb1.append(" Software");
StringBuffer
sb3=sb2.append(" Solution");
System.out.println(sb1);
System.out.println(sb2);
System.out.println(sb3);
}
}
Output:-
Sai
SaiSoftware
SaiSoftware Solution
SaiSoftware Solution
SaiSoftware Solution
SaiSoftware Solution

No comments:
Post a Comment