equals() in the class Object

Similar documents
The class Object. Lecture CS1122 Summer 2008

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Methods Common to all Classes

Programming Languages and Techniques (CIS120)

Super-Classes and sub-classes

java.lang.object: Equality

The Liskov Substitution Principle

Uguaglianza e Identità. (no, non avete sbagliato corso )

Object-Oriented Programming in the Java language

Creating an Immutable Class. Based on slides by Prof. Burton Ma

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

CSE 331 Software Design & Implementation

Expected properties of equality

CSE 331 Software Design & Implementation

Elementary Symbol Tables

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

Programming Languages and Techniques (CIS120)

COMP 250 Fall inheritance Nov. 17, 2017

Announcements. Equality. Lecture 10 Equality and Hashcode. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

Programming Languages and Techniques (CIS120)

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Programming Languages and Techniques (CIS120e)

For this section, we will implement a class with only non-static features, that represents a rectangle

Programming Languages and Techniques (CIS120)

Chapter 11: Collections and Maps

Equality. Michael Ernst. CSE 331 University of Washington

Equality. Michael Ernst. CSE 331 University of Washington

Software Engineering Design & Construction


MET08-J. Preserve the equality contract when overriding the equals() method

Binary Relations Part One

Principles of Software Construction: Objects, Design and Concurrency. Inheritance, type-checking, and method dispatch. toad

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

ELEMENTARY SEARCH ALGORITHMS

Equality in.net. Gregory Adam 07/12/2008. This article describes how equality works in.net

Introduction to Object-Oriented Programming


cs2010: algorithms and data structures

Programming Languages and Techniques (CIS120)

Effective Java. Object Equality. - Case Studies - Angelika Langer. Trainer/Consultant.

Advanced Topics on Classes and Objects

Principles of Software Construction: Objects, Design, and Concurrency. Testing and Object Methods in Java. Josh Bloch Charlie Garrod Darya Melicher

Binary Relations Part One

Programming Languages and Techniques (CIS120)

Java Fundamentals (II)

Charlie Garrod Michael Hilton

Java: advanced object-oriented features

Chapter 4 Defining Classes I

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt

ELEMENTARY SEARCH ALGORITHMS

MITOCW watch?v=kz7jjltq9r4

Not overriding equals

ELEMENTARY SEARCH ALGORITHMS

ELEMENTARY SEARCH ALGORITHMS

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Java Object Model. Or, way down the rabbit hole

CMSC131. Library Classes

Programming via Java Subclasses

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt

Domain-Driven Design Activity

Equality (2.1) Advanced Topics on Classes and Objects. Equality (1) Equality (2.2): Common Error

Building Java Programs. Inheritance and Polymorphism

The Java Type System (continued)

Object Oriented Programming. Week 7 Part 1 Exceptions

3.4 Hash Tables. hash functions separate chaining linear probing applications. Optimize judiciously

Implementing Object Equivalence in Java Using the Template Method Design Pattern

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

A Path-based Approach For Analyzing Object Equality in Java

3.4 Hash Tables. hash functions separate chaining linear probing applications

Lecture 10. Overriding & Casting About

Lecture 10 Declarations and Scope

Sets 1. The things in a set are called the elements of it. If x is an element of the set S, we say

CS 211: Methods, Memory, Equality

CSE 331 Final Exam 6/5/17. Name UW ID#

Inheritance and delega9on

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Selecting a Primary Key - 2

Principles of So3ware Construc9on: Objects, Design, and Concurrency. Introduc9on to Java. Josh Bloch Charlie Garrod Darya Melicher

CS 3114 Data Structures and Algorithms DRAFT Minor Project 3: PR Quadtree

C12a: The Object Superclass and Selected Methods

CIT 590 Homework 10 Battleship

1. Every program must have at least one class declaration. (*) 2. Every class declared in a program must have a distinct identifier.

Chapter 10. Learning Objectives

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Recursively Enumerable Languages, Turing Machines, and Decidability

Relations and Graphs

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.


Reflection/RMI 4/28/2009

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Code Reuse: Inheritance

CSE 331 Spring 2018 Midterm

7 Objects and Collections

Generics and collections

Lecture 3: Recursion; Structural Induction

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

The growth of functions. (Chapter 3)

Transcription:

equals() in the class Object 1 The Object class implements a public equals() method that returns true iff the two objects are the same object. That is: x.equals(y) == true iff x and y are (references to) the same object For some subclasses, this is adequate, especially for types for which the notion of an equality comparison doesn't really make practical sense.

Identity vs Equality 2 A deeper examination of the issue indicates there are two fundamentally distinct relationships at work, and that Object equals() conflates them: identity the relationship of being the same thing; x is identical to y iff x and y are the same object; in Java, this is tested by the operator == equality the relationship of having the same value; x is equal to y iff x and y, in some useful sense, have equivalent content; x and y may or may not be the same object; in Java, this is tested by the equals() method For many user-defined types, there are natural definitions of an equality relationship.

General Contract for equals() 3 The equals method implements an equivalence relation on non-null object references, equals() is: - reflexive: for any non-null reference value x, x.equals(x) should return true - symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true - transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true In addition: - it is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. - for any non-null reference value x, x.equals(null) should return false.

A User-defined Class 4 public Long offset; // offset of record in file public String record; // record contents public FileEntry(long offset, String data) { this.offset = offset; this.record = data; Here's a class that might be used in a program that accesses records from a file. It's certainly possible we might create two different FileEntry objects from the same record, in which case the notion of equals is different from identity.

Standard equals() Features We need to satisfy the general contract: public boolean equals(object other) { // Make sure there really IS another object; // something never equals nothing... if ( other == null ) return false; 5 // Make sure the other object is of the correct type: if (!this.getclass().equals(other.getclass()) ) return false;

Specialized equals() Features 6 We need to implement a sensible definition of what equality means for this type: public boolean equals(object other) { // Get a reference of the appropriate type: FileEntry o = (FileEntry) other; // Perform the type-specific test for equality: return ( this.offset.equals(o.offset) );

Complete Method 7 public boolean equals(object other) { // Make sure there really IS another object: if ( other == null ) return false; // Make sure it's of the correct type: if (!this.getclass().equals(other.getclass()) ) return false; // Get a reference of the appropriate type: FileEntry handle = (FileEntry) other; // Perform the type-specific test for equality: return ( this.offset.equals(handle.offset) );

A Debugging Hint When in doubt, let your code talk to you: public boolean equals(object other) { 8 System.out.println("Call made to FileEntry.equals()");