04.classes, objects, equality and cloning

24
1 ACM/JETT Workshop - August 4-5, 2005 04.Classes, Objects, Equality and Cloning

Upload: britanni-guerrero

Post on 03-Jan-2016

24 views

Category:

Documents


2 download

DESCRIPTION

04.Classes, Objects, Equality and Cloning. Topics. We will look at issues of comparing and copying objects. We will understand the Differences between comparing variables of primitive types and object-types . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 04.Classes, Objects, Equality and Cloning

1 ACM/JETT Workshop - August 4-5, 2005

04.Classes, Objects, Equality and Cloning

Page 2: 04.Classes, Objects, Equality and Cloning

2 ACM/JETT Workshop - August 4-5, 2005

Topics• We will look at issues of comparing and

copying objects.• We will understand the

– Differences between comparing variables of primitive types and object-types.

– Differences between copying variables of primitive types and object-types.

– Difference between Identity-equality and Content equality.

– Difference between Shallow copy and Deep copy– Concepts behind overriding equals() and clone()

methods

Page 3: 04.Classes, Objects, Equality and Cloning

3 ACM/JETT Workshop - August 4-5, 2005

Classes and ObjectsLet us once again, look at variable

declarations in Java and understand the meaning of types of variables before we talk about object equality.

Page 4: 04.Classes, Objects, Equality and Cloning

4 ACM/JETT Workshop - August 4-5, 2005

Classes and Objects int x = 5;

Book myBook = new Book(“Fun with Java”);

5

x

Fun with Java

myBook

x is a variable of a primitive type; Java primitive types such as char, int, double, boolean etc are capable pf storing simple information.

myBook is a reference variable that can hold a reference to an object.

An object’ type is determined by its class.

What is a reference? It is the memory address of the object.

memory

address

Page 5: 04.Classes, Objects, Equality and Cloning

5 ACM/JETT Workshop - August 4-5, 2005

Comparing “things” int x = 5;

int y = 5;

if (x == y) // true

Book myBook = new Book(“Fun with Java”);

Book yourBook = new Book(“Fun with Java”);

if (myBook == yourBook) // false

Why?

5

x

Fun with Java

myBook

y

5

yourBook

Fun with Java

Page 6: 04.Classes, Objects, Equality and Cloning

6 ACM/JETT Workshop - August 4-5, 2005

Equality by Identity using ==• The equality operator == returns true if and only if both its operands

have the same value.• Therefore, it works fine for variables of primitive types.• But reference variables contain memory addresses of objects.

• In case of reference variables, the == operator checks if the two objects are at the same location, ie. if they have the same identity

(Equality by identity)

• In our example, the reference variables myBook and yourBook contain different values; they point to independent Book objects.

• Therefore the comparison returns false.

Fun with Java Fun with Java

myBookyourBook

Page 7: 04.Classes, Objects, Equality and Cloning

7 ACM/JETT Workshop - August 4-5, 2005

Content Equality using equals()

• What if we want to check if two objects have the same state (content) ?

• To compare if two objects have the same state (content equality):

– we should define a method equals() for the class of objects that we are comparing.

Page 8: 04.Classes, Objects, Equality and Cloning

8 ACM/JETT Workshop - August 4-5, 2005

Object Equality

• All classes inherit the method equals() from the super class Object.

• In Object, the method only checks for equality by identity.

• But a class must override the equals() to check for content equality as it applies to its objects.

Page 9: 04.Classes, Objects, Equality and Cloning

9 ACM/JETT Workshop - August 4-5, 2005

• A java.lang.String class represents String objects.• A String object is a sequence of characters plus a set of

methods for manipulating strings.• Unlike other Java objects, Strings can have literals. • A String literal  is a sequence of zero or more characters

contained in double quotes. • Example:

String m1 = “hello”; String m2 = “hello”

• All occurrences of “hello” in a program refer to the same object.

hello

m1 m2

Comparing String objects

Page 10: 04.Classes, Objects, Equality and Cloning

10 ACM/JETT Workshop - August 4-5, 2005

String constructors can be used to create new String objects.

String s2 = new String(“hello");

String s3 = new String (“hello”);

hello

s2

s3

hello

Using String Constructors

Page 11: 04.Classes, Objects, Equality and Cloning

11 ACM/JETT Workshop - August 4-5, 2005

Comparing Strings

• Strings are compared according to their lexicographic order.

Some String Methods for Comparisons

// Overrides Object.equals()

public boolean equals(Object anObject); public boolean equalsIgnoreCase(String secondString)public int compareTo(String secondString)

Page 12: 04.Classes, Objects, Equality and Cloning

12 ACM/JETT Workshop - August 4-5, 2005

String Identity vs. String Equality

Two strings are equal if they have the same letters in the same order:

String s1 = new String (“hello”); String s2 = new String (“Hello”); String s3 = new String (“hello); s1.equals (s2); // false s2.equals (s1); // false s1.equalsIgnoreCase (s2); //true s1.equals (s3); // true s3.equals (s1); // true

Results based on content equality

Page 13: 04.Classes, Objects, Equality and Cloning

13 ACM/JETT Workshop - August 4-5, 2005

String Identity vs. String Equality String s1 = new String (“hello”); String s3 = new String (“hello);

s1.equals (s3); // true

s1 == s3; // false

Comparing content equality vs idenity equality

Page 14: 04.Classes, Objects, Equality and Cloning

14 ACM/JETT Workshop - August 4-5, 2005

String Identity vs. String Equality Using literal strings String s4 = “hello”; String s5 = “hello”; String s6 = new String(“hello”); s4 == s5; // true

Comparisons based on identity equality

s4

s5

hello hello

s6

s4 == s6; // false

S5 == s6; // false

Page 15: 04.Classes, Objects, Equality and Cloning

15 ACM/JETT Workshop - August 4-5, 2005

String Identity vs. String Equality Using literal strings String s4 = “hello”; String s5 = “hello”; String s6 = new String(“hello”); S4 == s5; // true S4.equals(s5); // true

Comparisons based on content equality

s4

s5

hello hello

s6

S4.equals(s6); // true

S5.equals(s6); // true

Page 16: 04.Classes, Objects, Equality and Cloning

16 ACM/JETT Workshop - August 4-5, 2005

An equals() for user-defined classes

• How do we define an equals() for a user-defined class?

• Let us see the code for equals() for a class Book.

Show class Book

Page 17: 04.Classes, Objects, Equality and Cloning

17 ACM/JETT Workshop - August 4-5, 2005

Note: Defining equals() is not simple

• There are more advanced issues related to implementing equals().

• If you override either the equals or hashCode methods from Object, you must almost certainly override the other method as well.

• Because of time constraints, we will not address these issues in the current series of lectures.

Page 18: 04.Classes, Objects, Equality and Cloning

18 ACM/JETT Workshop - August 4-5, 2005

Copying: Shallow copy vs Deep copy

int x = 5;

int y;

Y = x;

Book myBook = new Book(“Fun with Java”);

myBook.setAuthor(new Person(“Joan Smith”));

Book yourBook;

yourBook = myBook;

This is ShallowCopy

5

x

Fun with Java

myBook

y

5

yourBookJoan Smith

Page 19: 04.Classes, Objects, Equality and Cloning

19 ACM/JETT Workshop - August 4-5, 2005

Copying: Shallow copy vs Deep copy

Book myBook = new Book(“Fun with Java”);

myBook.setAuthor(new Person(“Joan Smith”));

Book yourBook;

yourBook = myBook;

What if we want to change the author of yourBook?

yourBook.setAuthor(new Person(“John Chen”));

Both Book instances

point to the

changed author.

Fun with Java

myBook

yourBookJohn Chen

Page 20: 04.Classes, Objects, Equality and Cloning

20 ACM/JETT Workshop - August 4-5, 2005

Copying: Shallow copy vs Deep copy

What if we want to copy a Book object and change the copy without affecting the original?

In other words, we want to have situation as shown below.

We need to copy the original object and the objects it refers to. This is called deep-copy.

Fun with Java

myBook

yourBook

Joan Smith

Fun with JavaJohn Chen

Page 21: 04.Classes, Objects, Equality and Cloning

21 ACM/JETT Workshop - August 4-5, 2005

What is a clone?

• A clone of an object is a new object that has the same state as the original.

• The clone and the cloned (original object) have different identities.

• In essence, you can modify the clone without affecting the original (Deep copy)

Page 22: 04.Classes, Objects, Equality and Cloning

22 ACM/JETT Workshop - August 4-5, 2005

clone() in Object• In order to provide an ability to deep-copy an

object, we need to override the clone() in Object.• Default implementation of the clone() method of

Object does ShallowCopy.– Ok for fields that are primitive types– Not Ok for cloning objects with reference fields.

• In order to clone objects of a class, the class must – implement the Cloneable interface– redefine the clone method– …

Page 23: 04.Classes, Objects, Equality and Cloning

23 ACM/JETT Workshop - August 4-5, 2005

Note: Defining clone() is not simple

• There are more advanced issues related to implementing clone().

• Because of time constraints, we will not address these issues in the current series of lectures.

Page 24: 04.Classes, Objects, Equality and Cloning

24 ACM/JETT Workshop - August 4-5, 2005

Test your understanding• At this point, you should be comfortable

with the– Differences between comparing variables of

primitive types and object-types.– Differences between copying variables of

primitive types and object-types.– Difference between using == and equals().– Difference between Shallow copy and Deep

copy– Concept behind overriding clone() method