Mutable and Immutable Classes in JAVA

download Mutable and Immutable Classes in JAVA

of 14

Transcript of Mutable and Immutable Classes in JAVA

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    1/14

    Mutable and

    immutableclasses

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    2/14

    AGENDA

    Introduction

    Mutable Classes

    Immutable Classes

    Cloning and Mutable

    Conclusion

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    3/14

    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.

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    4/14

    Immutable

    A class that contains methods (other thanconstructors) 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.

    Stringsare immutable class.

    In Java, objects are referred by references.

    If an object is known to be immutable, the

    object reference can be shared.

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    5/14

    For example, Boolean, Byte, Character,

    Double, Float, Integer, Long, Short, and Stringare 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 singlevalue that cannot change over the lifetime of

    the object.

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    6/14

    Sample Program

    Class Pro1

    {

    public static void main(String[] args)

    {

    String str = "WELCOME";

    System.out.println(str);

    str.toLowerCase(); //Doesnt impact on original content ofStr

    System.out.println(str); Output

    } WELCOME

    } WELCOME

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    7/14

    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

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    8/14

    Mutable

    A class that contains public methods or inputmethods 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.

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    9/14

    A String Buffer is a string that can be changed. StringBuffers are Mutable, theyre not inherently thread safe

    and thus many of the methods of String Buffer class

    are synchronized.

    The StringBuffer has the methods

    Append( )

    Insert ( )

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    10/14

    Mutable Objects in AWT and Swing

    Thejava.awt package defines several classes that encapsulategeometric information.

    AWT Geometry Classes

    CLASS DESCRIPTIONPoint (x , y) location in space

    Dimension Component width and

    height

    Insets Representation of the

    Borders of a container

    Rectangle Area in a coordinate

    space

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    11/14

    Thejava.awt.Component andjava.awt.Containerclasses 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);

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    12/14

    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 clones references to

    these objects with references to the copies.

  • 7/31/2019 Mutable and Immutable Classes in JAVA

    13/14

    Reference

    1. Absolute JAVA Walter Savitch

    2. Effective Java Joshua Bloch

    Websites

    www.sun.com

    www.google.com

    http://www.sun.com/http://www.google.com/http://www.google.com/http://www.sun.com/
  • 7/31/2019 Mutable and Immutable Classes in JAVA

    14/14