3. Introduction
An Immutable object is a kind of object whose
state cannot be modified after it is created where
a Mutable Object can be modified after it is
created.
4. Immutable
A class that contains methods (other than
constructors) that change any of the data in an
object of the class is called immutable classes
and object of the class are called immutable
objects.
Strings are immutable class.
In Java, objects are referred by references.
If an object is known to be immutable, the
object reference can be shared.
5. For example, Boolean, Byte, Character,
Double, Float, Integer, Long, Short, and String
are immutable classes in Java.
An immutable object is one whose externally
visible state cannot change after it is
instantiated.
The String, Integer, and BigDecimal classes in
the Java class library are examples of
immutable objects -- they represent a single
value that cannot change over the lifetime of
the object.
6. Sample Program
Class Pro1
{
public static void main(String[] args)
{
String str = "WELCOME";
System.out.println(str);
str.toLowerCase(); //Doesn’t impact on original content of Str
System.out.println(str); Output
} WELCOME
} WELCOME
7. Modified Program
class Pro1
{
public static void main(String[] args)
{
String str = “WELCOME";
System.out.println(str);
String str1 = str.toLower();
System.out.println(str1); Output
} WELCOME
} welcome
8. Mutable
A class that contains public methods or input
methods that can change the data in an object
of the class is called mutable classes and the object
of the class are called mutable objects.
The class Date is an example of a mutable class.
When defining any methods , that method should
not return a reference to a mutable object.
Instead use a copy constructor to return a
reference to a completely independent copy of
the mutable object.
9. A String Buffer is a string that can be changed. String
Buffers are Mutable , they’re not inherently thread safe
and thus many of the methods of String Buffer class
are synchronized.
The StringBuffer has the methods
Append( )
Insert ( )
10. Mutable Objects in AWT and Swing
The java.awt package defines several classes that encapsulate
geometric information.
AWT Geometry Classes
CLASS DESCRIPTION
Point (x , y) location in space
Dimension Component width and
height
Insets Representation of the
Borders of a container
Rectangle Area in a coordinate
space
11. The java.awt.Component and java.awt.Container
classes define methods to access certain geometric
information.
public Point getLocation();
public void setLocation( Point loc);
public Dimension getSize();
public void setSize(Dimension size);
public Insets getInsets();
public void setInsets(Insets insets);
public Rectangle getBounds();
public void setBounds(Rectangle bounds);
12. Cloning and mutable objects
All classes that implement Cloneable should override
clone with a public method whose return type is the
class itself.
This method should call super.clone and then fix any
fields that need to be fixed.
Typically this means copying any mutable objects
that comprise the internal deep structure of object
being cloned and replacing the clone’s references to
these objects with references to the copies.