1 / 24

04.Classes, Objects, Equality and Cloning

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 .

Download Presentation

04.Classes, Objects, Equality and Cloning

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 04.Classes, Objects, Equality and Cloning

  2. Topics • We will look at issues of comparing and copying objects. • We will understand the • Differences between comparing variables of primitivetypes and object-types. • Differences between copying variables of primitivetypes 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. 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. Classes and Objects int x = 5; Book myBook = new Book(“Fun with Java”); x is a variable of a primitive type; Java primitive types such as char, int, double, boolean etc are capable pf storing simple information. 5 x memory address Fun with Java myBook 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.

  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 5 x y Fun with Java Fun with Java myBook yourBook

  6. 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 myBookandyourBook contain different values; they point to independent Book objects. • Therefore the comparison returns false. Fun with Java Fun with Java myBook yourBook

  7. Content Equality using equals() • What if we want to check if two objects have the samestate (content) ? • To compare if two objects have the samestate (content equality): • we should define a method equals() for the class of objects that we are comparing.

  8. 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. Comparing String objects • 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 m2 m1

  10. Using String Constructors String constructors can be used to create new String objects. String s2 = new String(“hello"); String s3 = new String (“hello”); hello s2 hello s3

  11. 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. 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. 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. String Identity vs. String Equality Using literal strings String s4 = “hello”; String s5 = “hello”; String s6 = new String(“hello”); s4 == s5; // true hello hello s4 s6 s4 == s6; // false S5 == s6; // false s5 Comparisons based on identity equality

  15. 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 hello hello s4 s6 S4.equals(s6); // true S5.equals(s6); // true s5 Comparisons based on content equality

  16. 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. 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. 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 5 x y Fun with Java myBook Joan Smith yourBook

  19. 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 John Chen yourBook

  20. Copying: Shallow copy vs Deep copy What if we want to copy a Book object and change the copywithout 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 Joan Smith myBook Fun with Java John Chen yourBook

  21. 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. 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 Objectdoes 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. 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. Test your understanding • At this point, you should be comfortable with the • Differences between comparing variables of primitivetypes and object-types. • Differences between copying variables of primitivetypes and object-types. • Difference between using == and equals(). • Difference between Shallow copy and Deep copy • Concept behind overriding clone() method

More Related