SlideShare a Scribd company logo
4
Most read
7
Most read
10
Most read
Mutable and
immutable
  classes
AGENDA

• Introduction

• Mutable Classes

• Immutable Classes

• Cloning and Mutable

• Conclusion
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.
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.
 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.
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
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
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.
 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 ( )
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
 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);
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.
Reference

1. Absolute JAVA – Walter Savitch
2. Effective Java – Joshua Bloch


Websites

www.sun.com
www.google.com
Mutable and  immutable classes

More Related Content

PPTX
Method overloading
PPTX
Object Oriented Programming in Python
PPTX
Variables in python
PPTX
Type casting in java
PDF
Introduction to Python
PPTX
JAVA AWT
PPTX
Classes objects in java
PPTX
Data types in c++
Method overloading
Object Oriented Programming in Python
Variables in python
Type casting in java
Introduction to Python
JAVA AWT
Classes objects in java
Data types in c++

What's hot (20)

PDF
Immutable vs mutable data types in python
PDF
Python programming : Files
PPT
Method overriding
DOC
Arrays and Strings
PDF
Oops concepts || Object Oriented Programming Concepts in Java
PPTX
Python variables and data types.pptx
PPT
Strings
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Exception handling
PPSX
Break and continue
PPT
9. Input Output in java
PPTX
Dynamic memory allocation
PPT
Applet life cycle
PPSX
Type conversion
PPTX
Classes, objects in JAVA
PPTX
Fundamentals of Python Programming
PPTX
PPTX
Type conversion
PPTX
Abstract Class Presentation
PPTX
Branching statements
Immutable vs mutable data types in python
Python programming : Files
Method overriding
Arrays and Strings
Oops concepts || Object Oriented Programming Concepts in Java
Python variables and data types.pptx
Strings
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Exception handling
Break and continue
9. Input Output in java
Dynamic memory allocation
Applet life cycle
Type conversion
Classes, objects in JAVA
Fundamentals of Python Programming
Type conversion
Abstract Class Presentation
Branching statements
Ad

Viewers also liked (20)

PPTX
Trends and technologies in system softwares
PPT
09 binary-trees
PPTX
14.jun.2012
PPT
Constants
PPTX
Graph theory
PPTX
More on Lex
DOC
What are interpersonal skills
PPTX
Linear programming problem
PPTX
Investment problem
PPTX
Set data structure 2
PPTX
Buddy system final
PPTX
Set data structure
PPTX
Storage Class Specifiers in C++
PPTX
Graph data structure
PPTX
Inline function
PPTX
E post office system
PPTX
Combined paging and segmentation
PPTX
Linkers
PPTX
Uid
PPTX
Spss
Trends and technologies in system softwares
09 binary-trees
14.jun.2012
Constants
Graph theory
More on Lex
What are interpersonal skills
Linear programming problem
Investment problem
Set data structure 2
Buddy system final
Set data structure
Storage Class Specifiers in C++
Graph data structure
Inline function
E post office system
Combined paging and segmentation
Linkers
Uid
Spss
Ad

Similar to Mutable and immutable classes (20)

PPTX
Ch-2ppt.pptx
PPTX
Android Training (Java Review)
PPTX
Basic concept of class, method , command line-argument
PPTX
classes-objects in oops java-201023154255.pptx
PDF
Java defining classes
PPTX
Java basics
PDF
Lecture 8 Library classes
PPTX
Unit3 part1-class
PPTX
Java As an OOP Language,Exception Handling & Applets
PPTX
‫Object Oriented Programming_Lecture 3
PPTX
PPT Lecture-1.4.pptx
PPTX
class as the basis.pptx
PPTX
Class and Object.pptx from nit patna ece department
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
DOCX
Java mcq
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PDF
Java Programming - 04 object oriented in java
PPTX
C# classes objects
PPTX
Unit-1 Data Types and Operators.pptx to computers
PDF
Wrapper classes
Ch-2ppt.pptx
Android Training (Java Review)
Basic concept of class, method , command line-argument
classes-objects in oops java-201023154255.pptx
Java defining classes
Java basics
Lecture 8 Library classes
Unit3 part1-class
Java As an OOP Language,Exception Handling & Applets
‫Object Oriented Programming_Lecture 3
PPT Lecture-1.4.pptx
class as the basis.pptx
Class and Object.pptx from nit patna ece department
Class and Object JAVA PROGRAMMING LANG .pdf
Java mcq
classandobjectunit2-150824133722-lva1-app6891.ppt
Java Programming - 04 object oriented in java
C# classes objects
Unit-1 Data Types and Operators.pptx to computers
Wrapper classes

More from Tech_MX (20)

PPTX
Virtual base class
PPTX
Theory of estimation
PPTX
Templates in C++
PPT
String & its application
PPTX
Statistical quality__control_2
PPTX
Stack data structure
PPT
Stack Data Structure & It's Application
PPTX
Spanning trees & applications
PPTX
Real time Operating System
PPTX
Parsing
PPTX
Mouse interrupts (Assembly Language & C)
PPT
Motherboard of a pc
PPTX
MultiMedia dbms
PPTX
Merging files (Data Structure)
PPTX
Memory dbms
PPTX
Linear regression
PPTX
Keyboard interrupt
PPTX
Introduction to loaders
PPT
Interpersonal communication
PPTX
Interesting applications of graph theory
Virtual base class
Theory of estimation
Templates in C++
String & its application
Statistical quality__control_2
Stack data structure
Stack Data Structure & It's Application
Spanning trees & applications
Real time Operating System
Parsing
Mouse interrupts (Assembly Language & C)
Motherboard of a pc
MultiMedia dbms
Merging files (Data Structure)
Memory dbms
Linear regression
Keyboard interrupt
Introduction to loaders
Interpersonal communication
Interesting applications of graph theory

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPT
Teaching material agriculture food technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Cloud computing and distributed systems.
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
Understanding_Digital_Forensics_Presentation.pptx
sap open course for s4hana steps from ECC to s4
“AI and Expert System Decision Support & Business Intelligence Systems”
Teaching material agriculture food technology
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
MIND Revenue Release Quarter 2 2025 Press Release
Spectral efficient network and resource selection model in 5G networks
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Per capita expenditure prediction using model stacking based on satellite ima...
Cloud computing and distributed systems.
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx

Mutable and immutable classes

  • 2. AGENDA • Introduction • Mutable Classes • Immutable Classes • Cloning and Mutable • Conclusion
  • 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.
  • 13. Reference 1. Absolute JAVA – Walter Savitch 2. Effective Java – Joshua Bloch Websites www.sun.com www.google.com