Chapter 14' Assigning and comparing objects - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Chapter 14' Assigning and comparing objects

Description:

mike == miranda; //false -15 - Use of equal for equality of objects: Shallow equality (1/2) ... miranda. instance. mike.equals(mike); //true. mike.equals ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 17
Provided by: shc4
Category:

less

Transcript and Presenter's Notes

Title: Chapter 14' Assigning and comparing objects


1
  • Chapter 14. Assigning and comparing objects

2
Introduction
  • 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

3
Assignment deep and shallow copy
4
Comparison for equality
5
Assignment 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())
6
Assignment 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

7
Making 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()
8
An 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
9
An 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())

10
mike
50.00
instance
Account
public Object clone()
Deep_copy_mike
clone
50.00
11
Duplicating 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
12
Deep 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
13
Use of for equality of objects Deep equality
(1/2)
  • The normal operator implements deep equality
    of objects

//true //false //false
14
Use 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
15
Use 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
16
Use 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
Write a Comment
User Comments (0)
About PowerShow.com