Title: Chapter 14' Assigning and comparing objects
1- Chapter 14. Assigning and comparing objects
2Introduction
- Objects are represented by a handle, that
reference the storage of the object - The operation of assignment and comparison for
equality may be performed in two distinct ways
3Assignment deep and shallow copy
4Comparison for equality
5Assignment of object (1/2)
- In java the normal assignment operator
implements a shallow copy of an object - Remember in Java all objects are represented by a
handle or pointer to the object storage
public static void main(String args)
Account mike new Account(50.0) Account
shallow_copy_mike mike mike.deposit(100.00)
System.out.println(mike.account_balance())
System.out.println(shallow_copy_mike.account_bal
ance())
6Assignment of object (2/2)
- A shallow copy the storage for the object is not
duplicated whereas in a deep copy the storage is - If a shallow copy of an object is made any
changes to the object will be reflected in all
the shallow copies
7Making an object cloneable Deep copy
- To make an instance of a class cloneable a class
must - Implement the interface Cloneable
- Override the method clone in the class Object
public interface Cloneable
protected Object clone() throws
CloneNotSupportedException
- Is required to provide a method clone
- Not be able to see the method clone in the
superclass Object if it were not overridden with
a public version of the method
public Object clone()
8An example (1/2)
class Account implement Cloneable private
double the_balance 0.0d private double
the_min_balance 0.0d // code for
constructors public Object clone()
//Object o null try
return super.clone() // o super.clone()
catch (CloneNotSupportedException
e) System.exit(-1)
return new Object() // return o
//code for the method // account_balance,
withdraw, deposit, set_min_balance
9An example (2/2)
class Main public static void main(String
args) Account mike new
Account(50.00) Account deep_copy_mike
(Account)mike.clone()
mike.deposit(100.00) System.out.println(
Balance account mike mike.account_balance())
System.out.println(Balance account
deep copy mike
deep_copy_mike.account_balance())
10mike
50.00
instance
Account
public Object clone()
Deep_copy_mike
clone
50.00
11Duplicating the instance variables of a class
directly
- The body of the method clone can be used to copy
the individual instance variables of the class
directly - If any of the instance variables are themselves
objects then they are deep copied using their
clone method
class Account implements Cloneable private
double the_balance 0.0d private double
the_min_balance 0.0d public Object clone()
Account copy new Account()
copy.the_balance the_balance
copy.the_min_balance the_min_balance
return copy //code for the methods
constructors, account_balance, withdraw,
deposit
12Deep and shallow equality
- If the objects share the same storage, they are
deeply equal - If the objects do not share the same storage, but
they do contain the same state information, then
they are shallow equal
Deep copy
Shallow copy
13Use of for equality of objects Deep equality
(1/2)
- The normal operator implements deep equality
of objects
//true //false //false
14Use of for equality of objects Deep equality
(2/2)
mike
50.00
Account
corinna
instance
50.00
miranda
mike mike //true mike corinna
//false mike miranda //false
100.00
15Use of equal for equality of objects Shallow
equality (1/2)
- By overriding the method equal(in the class
Object) the user can implement appropriate code
to test the equality of the two objects
//true //true //false
16Use of equal for equality of objects Shallow
equality(2/2)
mike
50.00
Account
corinna
instance
50.00
miranda
mike.equals(mike) //true mike.equals(corinna)
//true mike.equals(miranda) //false
100.00