38
2016, Marcus Biel, http://www.marcus-biel.com/ Marcus Biel, Software Craftsman Identity vs Equality in Java http://www.marcus-biel.com/identity-vs-equality-in-ja va-video/

Identity vs Equality in Java by Software Craftsman Marcus Biel

Embed Size (px)

Citation preview

Page 1: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Marcus Biel, Software Craftsman

Identity vs Equality in Java

http://www.marcus-biel.com/identity-vs-equality-in-java-video/

Page 2: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

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.

Page 3: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

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.

Page 4: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Identity vs Equality

To be able to locate an object, every object is assigned an address in the memory.

Page 5: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

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”.

Page 6: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

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”?

Page 7: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

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.

Page 8: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Identity Operator

a == b

To check for object identity, you use the “equals“ operator.

Page 9: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding Example

To illustrate this, let’s do a little coding example.

Page 10: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar myCar1 myCar1

We create a reference variable of type Car with name “myCar1”…

Page 11: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar myCar1 = new Car("blue"); myCar1

Car Object 1

…and assign it a Car Object.

Page 12: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar myCar1 = new Car("blue");Car myCar2

myCar1

myCar2Car Object 1

Now we create a second Car reference variable named “myCar2”…

Page 13: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar myCar1 = new Car("blue");Car myCar2 = myCar1;

myCar1

myCar2Car Object 1

…and assign it the value of the reference variable “myCar1”.

This way, both “myCar1” and “myCar2” reference the same Object in memory.

Page 14: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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”…

Page 15: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 16: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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?

Page 17: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 18: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 19: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 20: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 21: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 22: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 23: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

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.

Page 24: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Equality

However, in other cases we just want to know – is this a white house?…

Page 25: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

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.

Page 26: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

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.

Page 27: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 28: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 29: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 30: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 31: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 32: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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?

Page 33: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 34: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Coding ExampleCar 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.

Page 35: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

hashCode and equals @Override public int hashCode() { return color.hashCode(); }

@Override 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.

Page 36: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

hashCode and equals

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.

@Override public int hashCode() { return color.hashCode(); }

@Override 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); }

Page 37: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

hashCode and equals

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.

@Override public int hashCode() { return color.hashCode(); }

@Override 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); }

Page 38: Identity vs Equality in Java by Software Craftsman Marcus Biel

2016, Marcus Biel, http://www.marcus-biel.com/

Copyright © 2016

Marcus BielAll rights reserved

http://www.marcus-biel.com