 2016, Marcus Biel, Marcus Biel, Software Craftsman Identity vs Equality in Java

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Identity and Equality Based on material by Michael Ernst, University of Washington.
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Lecture 12: Using Classes Yoni Fridman 7/19/01 7/19/01.
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.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
String Escape Sequences
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Today’s lecture Review chapter 5 go over exercises.
Copyright Pearson Prentice-Hall Why?
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
“Unboxing” means taking an Integer object and assigning its value to a primitive int. This is done using the.intValue( ) method. Example; Integer z = new.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
More on Objects Static and equals.. Each object takes memory Each time you make an object of a given class – you create a space in memory. E.g. public.
EGR 2261 Unit 10 Two-dimensional Arrays
Debugging and Random Numbers
Lecture 17: Polymorphism (Part II)
Primitive Types Vs. Reference Types, Strings, Enumerations
Variables as Remote Control
String Objects & its Methods
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Marcus Biel, Software Craftsman
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
CSE 403 JUnit Reading: These lecture slides are copyright (C) Marty Stepp, They may not be rehosted, sold, or modified without expressed permission.
Building Java Programs
Focus of the Course Object-Oriented Software Development
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
CSE 373 Separate chaining; hash codes; hash maps
Classes, Objects and Methods
References Revisted (Ch 5)
Controlling Program Flow
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
String Objects & its Methods
Presentation transcript:

 2016, Marcus Biel, Marcus Biel, Software Craftsman Identity vs Equality in Java

 2016, Marcus Biel, Identity vs Equality All objects a program creates while running are stored in the memory of your computer. This memory consists of billions of little cells, where each cell can either store a one or a zero, basically.

 2016, Marcus Biel, Identity vs Equality When a Java program is running, it usually creates a large number of different objects in the memory. Depending on the size of an object, it will occupy some of the available space.

 2016, Marcus Biel, Identity vs Equality To be able to locate an object, every object is assigned an address in the memory.

 2016, Marcus Biel, Identity vs Equality To illustrate this, let’s say our little blue object lives at the address “Sixteen hundred Pennsylvania Avenue North West, Washington DC”.

 2016, Marcus Biel, Identity Now when you compare this object with another object, sometimes you want to know - Is this the object at “Sixteen hundred Pennsylvania Avenue North West, Washington DC”?

 2016, Marcus Biel, Identity In other words, is this THE White House? That is what object identity means: one unique object at one specific address in the memory.

 2016, Marcus Biel, Identity Operator a == b To check for object identity, you use the “equals“ operator.

 2016, Marcus Biel, Coding Example To illustrate this, let’s do a little coding example.

 2016, Marcus Biel, Coding Example Car myCar1 myCar1 We create a reference variable of type Car with name “myCar1”…

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); myCar1 Car Object 1 …and assign it a Car Object.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 myCar1 myCar2 Car Object 1 Now we create a second Car reference variable named “myCar2”…

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; myCar1 myCar2 Car Object 1 …and assign it the value of the reference variable “myCar1”. This way, both “myCar1” and “myCar2” reference the same Object in memory.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 myCar1 myCar2 myCar3 Car Object 1 And finally, we create a third Car reference variable called “myCar3”…

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); myCar1 myCar2 myCar3 Car Object 2 Car Object 1 …and assign it a new Car object. So now we have two blue car objects and three reference variables of type car, referencing those two objects.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Now, what will happen if we compare the three reference variables with the equals operator?

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1 == myCar1){ } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 What will myCar1 equals operator myCar1 return? Pause the slide and think about it, before you continue.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1 == myCar1){ // true } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Comparing a reference variable with itself will of course return true.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1 == myCar1){ // true } if(myCar1 == myCar2){ } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Next, we compare “myCar1” with “myCar2”. Pause the slide again and think about it.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1 == myCar1){ // true } if(myCar1 == myCar2){ // true } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Both reference variables myCar1 and myCar2 reference the same object in the memory, so this will also return true.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1 == myCar1){ // true } if(myCar1 == myCar2){ // true } if(myCar1 == myCar3){ } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Finally, we compare “myCar1” to “myCar3”. What will this return? Pause the slides and think about it.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1 == myCar1){ // true } if(myCar1 == myCar2){ // true } if(myCar1 == myCar3){ // false } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 “myCar1” and “myCar3” both reference a blue Car. But each variable references a DIFFERENT object in memory. Therefore, the equals operator will return false.

 2016, Marcus Biel, Identity So Object Identity will tell us if two reference variables reference the same Object in memory, or the same unique house as in our example.

 2016, Marcus Biel, Equality However, in other cases we just want to know – is this a white house?…

 2016, Marcus Biel, Equality … or a blue house? In other words, do we consider this house equal to the other house we are searching for? This is what Object equality means.

 2016, Marcus Biel, Equals method a.equals(b) To check for object equality, you use the “equals” method. To get a deeper understanding, let’s go back to our coding example.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1.equals(myCar1)){ } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 What will myCar1 equals myCar1 return? Pause the slide and think about it.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1.equals(myCar1)){ // true } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Comparing a reference variable with itself will return true, just as before with the equals operator.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1.equals(myCar1)){ // true } if(myCar1.equals(myCar2)){ } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Next, we compare “myCar1” with “myCar2”. Pause the slide and think about the result.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1.equals(myCar1)){ // true } if(myCar1.equals(myCar2)){ // true } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 This will also return true, as we are still comparing the same object in memory.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1.equals(myCar1)){ // true } if(myCar1.equals(myCar2)){ // true } if(myCar1.equals(myCar3)){ } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Finally, we compare the two blue cars myCar1 and myCar3. What will this return? Pause the slide and think about it once again.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1.equals(myCar1)){ // true } if(myCar1.equals(myCar2)){ // true } if(myCar1.equals(myCar3)){ // false } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Unlike you probably thought, this still returns false, even though we compare two equal blue cars. Why is that?

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1.equals(myCar1)){ // true } if(myCar1.equals(myCar2)){ // true } if(myCar1.equals(myCar3)){ // false } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Well, first of all you have to tell your program, that “YOU” consider two blue cars as equal.

 2016, Marcus Biel, Coding Example Car myCar1 = new Car("blue"); Car myCar2 = myCar1; Car myCar3 = new Car("blue"); if(myCar1.equals(myCar1)){ // true } if(myCar1.equals(myCar2)){ // true } if(myCar1.equals(myCar3)){ // false } myCar1 myCar2 myCar3 Car Object 2 Car Object 1 Because it totally depends on what “YOU” consider equal or unequal. And you have to express that in code.

 2016, Marcus Biel, hashCode and public int hashCode() { return color.hashCode(); public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Car other = (Car) obj; return this.color.equals(other.color); } In short, you do that by overwriting the two methods hashCode and equals.

 2016, Marcus Biel, hashCode and public int hashCode() { return color.hashCode(); public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Car other = (Car) obj; return this.color.equals(other.color); } If you don’t overwrite these two methods, you get the default behaviour as defined in the class Object. The default behaviour of hashCode and equals is actually the same as for Object identity.

 2016, Marcus Biel, hashCode and public int hashCode() { return color.hashCode(); public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Car other = (Car) obj; return this.color.equals(other.color); } This is not because the Java creators thought that would be a good idea – just because they didn't have any other option. When they wrote the class Object our Car class didn’t exist yet.

 2016, Marcus Biel, Copyright © 2016 Marcus Biel All rights reserved