Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ACM/JETT Workshop - August 4-5, 2005 1 04.Classes, Objects, Equality and Cloning."— Presentation transcript:

1 ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning

2 ACM/JETT Workshop - August 4-5, 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

3 ACM/JETT Workshop - August 4-5, Classes and Objects Let us once again, look at variable declarations in Java and understand the meaning of types of variables before we talk about object equality.

4 ACM/JETT Workshop - August 4-5, 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

5 ACM/JETT Workshop - August 4-5, 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

6 ACM/JETT Workshop - August 4-5, 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 myBook yourBook

7 ACM/JETT Workshop - August 4-5, 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.

8 ACM/JETT Workshop - August 4-5, 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.

9 ACM/JETT Workshop - August 4-5, 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

10 ACM/JETT Workshop - August 4-5, 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

11 ACM/JETT Workshop - August 4-5, 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)

12 ACM/JETT Workshop - August 4-5, 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

13 ACM/JETT Workshop - August 4-5, 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

14 ACM/JETT Workshop - August 4-5, 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 s6 s4 == s6 ; // false S5 == s6; // false

15 ACM/JETT Workshop - August 4-5, 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 s6 S4.equals(s6); // true S5.equals(s6); // true

16 ACM/JETT Workshop - August 4-5, 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

17 ACM/JETT Workshop - August 4-5, 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.

18 ACM/JETT Workshop - August 4-5, 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 yourBook Joan Smith

19 ACM/JETT Workshop - August 4-5, 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 yourBook John Chen

20 ACM/JETT Workshop - August 4-5, 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 Java John Chen

21 ACM/JETT Workshop - August 4-5, 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)

22 ACM/JETT Workshop - August 4-5, 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 –…

23 ACM/JETT Workshop - August 4-5, 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.

24 ACM/JETT Workshop - August 4-5, 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


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

Similar presentations


Ads by Google