Equality, conclusion Based on material by Michael Ernst, University of Washington.

Slides:



Advertisements
Similar presentations
Identity and Equality Based on material by Michael Ernst, University of Washington.
Advertisements

Reasoning About Code; Hoare Logic, continued
== equals ? CSE 331 SOFTWARE DESIGN & IMPLEMENTATION EQUALITY Autumn 2011.
Abstraction Functions. Announcements Exam 1 on Tuesday March 3 rd Closed book/phone/laptop 2 cheat pages allowed (handwritten or typed) 1 double-sided.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Slides by Vinod Rathnam with material from Alex Mariakakis Kellen Donohue, David Mailhot, and Hal Perkins Midterm Review.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Equality CSE 331 University of Washington. Object equality A simple idea - we have intuitions about equality: - Two objects are equal if they have the.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Data structures and algorithms in the collection framework 1 Part 2.
Not overriding equals  what happens if you do not override equals for a value type class?  all of the Java collections will fail in confusing ways 1.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 ==, equals(), and all that (Slides by David Notkin and Mike Ernst) 1.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Data Abstractions EECE 310: Software Engineering.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
Polymorphism Liskov 8. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism.
Difficult Specifications in Javari Immutability Inference Jaime Quinonez Program Analysis Group April 20, 2007.
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
Polymorphism (generics) CSE 331 University of Washington.
1 CSE 331 Hash codes; annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Polymorphism SWE 619. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism Element.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
1 CSE 331 The Object class; Object equality and the equals method slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
Slides with material from Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 3: HW4, ADTs, and more.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Representation Invariants and Abstraction Functions.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
Testing (final thoughts). equals() and hashCode() Important when using Hash-based containers class Duration { public final int min; public final int sec;
 2016, Marcus Biel, Marcus Biel, Software Craftsman Identity vs Equality in Java
Slides by Alex Mariakakis Section 5: Midterm Review.
EECE 310: Software Engineering
COMP 14 Introduction to Programming
Chapter 19 Java Data Structures
CSE 116/504 – Intro. To Computer Science for Majors II
CSE 331 Software Design & Implementation
The hashCode method.
Cse 373 May 15th – Iterators.
Subtype Polymorphism, Subtyping vs
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Testing, conclusion Based on material by Michael Ernst, University of Washington.
Representation Invariants and Abstraction Functions
Abstraction functions, Reasoning About ADTs
Testing, cont., Equality and Identity
CSE 331 Software Design and Implementation
Reasoning About Code; Hoare Logic
Reasoning about Loops, Conclusion
Specifications, conclusion. Abstract Data Types (ADTs)
Exam 1 Review.
Section 6: Midterm Review
Implementing Non-Static Features
CSE373: Data Structures & Algorithms Lecture 13: Hash Tables
Subtype Polymorphism, Subtyping vs
CSE 331 Software Design and Implementation
Sets, Maps and Hash Tables
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
The programmer returns with 12 loaves of bread.
Michael Ernst CSE 140 University of Washington
EECE 310: Software Engineering
CSE 373 Separate chaining; hash codes; hash maps
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CS2013 Lecture 7 John Hurley Cal State LA.
CSE373: Data Structures & Algorithms Lecture 13: Hash Tables
Ruth Anderson UW CSE 160 Spring 2018
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

Equality, conclusion Based on material by Michael Ernst, University of Washington

Announcements HW4 is due tomorrow at 11:59:59 pm The total number of late days has been increased to 7. Up to 3 late days for HW4, up to 2 on other assignments HW4 Resubmit will be out on Wednesday, March 14, 2018. Optional! May only improve your HW4 grade Only the autograded part Final score will be the average of HW4 and HW4 Resubmit autograded parts Due Tuesday, March 20, 2018 at 1:59:59 pm HW5 based off of HW4, will be out on Wednesday, March 14, 2018. Due Thursday, March 22, 2018 at 1:59:59 pm.

Control Flow Graph Shows all paths that might be traversed though the code during execution Each node is a basic block Edged are jumps in the control flow For every edge A → B: Outdegree of A > 1 Or indegree of B > 1 (or both) Start with the full flow graph and do edge contraction Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Outline Reference equality “Value” equality with .equals Equality and inheritance equals and hashCode Equality and mutation Implementing equals and hashCode efficiently Equality and ADTs Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Equality Simple idea: Many subtleties 2 objects are equal if they have the same value Many subtleties Same rep or same abstract value? Remember the HW3 questions Equality in the presence of inheritance? Does equality hold just now or is it eternal? How can we implement equality efficiently? Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Equality, Mutation and Time If two objects are equal now, will they always be equal? In mathematics, the answer is “yes” In Java, the answer is “you chose” The Object spec does not specify this For immutable objects Value never changes, equality is eternal For mutable objects We can either compare values now, or be eternal (can’t have both since value can change) Spring 18 CSCI 2600, K. Kuzmin, A Milanova (slide by Michael Ernst)

StringBuffer Example StringBuffer is mutable, and takes the eternal approach StringBuffer s1 = new StringBuffer(“hello”); StringBuffer s2 = new StringBuffer(“hello”); System.out.println(s1.equals(s1)); // true System.out.println(s1.equals(s2)); // false equals is just reference equality (==). This is the only way to ensure eternal equality for StringBuffer Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on slide by Michael Ernst)

Date Example Date is mutable, and takes the “compare values now” approach Date d1 = new Date(0); //Jan 1, 1970 00:00:00 GMT Date d2 = new Date(0); System.out.println(d1.equals(d2)); // true d2.setTime(1); //a millisecond later System.out.println(d1.equals(d2)); // false Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on slide by Michael Ernst)

Equality and Mutation We can violate rep invariant of a Set container (rep invariant: there are no duplicates in set) by mutating after insertion Set<Date> s = new HashSet<Date>(); Date d1 = new Date(0); Date d2 = new Date(1); s.add(d1); s.add(d2); d2.setTime(0); // mutation after d2 already in the Set! for (Date d : s) { System.out.println(d); }

Equality and Mutation Sets assume hash codes don’t change Set<Date> s = new HashSet<Date>(); Date d1 = new Date(0); Date d2 = new Date(1000); // 1 sec later s.add(d1); s.add(d2); d2.setTime(10000); s.contains(d2); s.contains(new Date(10000)); s.contains(new Date(1000)); // false // false // false again Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Equality and Mutation Be very careful with elements of Sets Ideally, elements will be immutable objects Java spec for Sets warns about using mutable objects as set elements Same problem applies to keys in maps Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Outline Reference equality “Value” equality with .equals Equality and inheritance equals and hashCode Equality and mutation Implementing equals and hashCode efficiently Equality and ADTs Spring 18 CSCI 2600, K. Kuzmin, A Milanova

The int hashCode Method hashCode computes an index for the object (to be used in hashtables) Javadoc for Object.hashCode(): … Must be consistent with equals() method: a.equals(b) => a.hashCode() == b.hashCode() RULE: If you override equals() make sure you override hashCode to make it consistent with equals()!!!

Implementing equals Efficiently equals can be expensive! How can we speed-up equals? public boolean equals(Object o) { if (this == o) return true; // class-specific prefiltering (e.g., // compare file size if working with files) // Lastly, compare fields (can be expensive) } Can we use hashCode to prefilter? Why yes, or why not?

Example: A Naïve Poly.equals // Assumes int arrays rep and rep inv: c[c.length-1]!=0 public boolean equals(Object o) { if (o instanceof Poly) { Poly p = (Poly) o; int i=0; while (i < Math.min(p.c.length,c.length)) { if (p.c[i] != c[i]) // Assume int arrays return false; i = i+1; } if (i != p.c.length || i != c.length) return false; return true; else

Example: Better equals public boolean equals(Object o) { if (o instanceof Poly) { Poly p = (Poly) o; if (p.c.length != c.length) return false; // prefiltering for (int i=0; i < c.length; i++) { if (p.c[i] != c[i]) return false; } return true; else Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Implementing hashCode // returns: the hashCode value of this String public int hashCode() { int h = this.hash; // rep. field hash if (h == 0) { // caches the hashcode char[] val = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[i]; } this.hash = h; } return h; } For immutable objects, we can use hashCode to prefilter. This works only for immutable objects! Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Outline Testing Equality White-box testing Reference equality “Value” equality with .equals Equality and inheritance equals and hashCode Equality and mutation Equality in ADTs Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Rep Invariant, AF and Equality With ADTs we compare abstract values, not rep Usually, many valid reps map to the same abstract value If Concrete Object (rep) and Concrete Object’ (rep’) map to the same Abstract Value, then Concrete Object and Concrete Object’ must be equal A stronger rep invariant shrinks the domain of the AF and simplifies equals

Example: Line Segment class LineSegment { // Rep invariant: // !(x1=x2 && y1=y2) float x1,y1; float x2,y2; … } // equals must // return true for // {x1:1,y1:2,x2:4,y2:5} // and {4,5,1,2} class LineSegment { // Rep invariant: // x1<x2 || // x1=x2 && y1<y2 float x1,y1; float x2,y2; … } // equals is simpler: // {4,5,1,2} is not // valid rep anymore Spring 18 CSCI 2600, K. Kuzmin, A Milanova