identity vs equality in java by software craftsman marcus biel
TRANSCRIPT
![Page 1: Identity vs Equality in Java by Software Craftsman Marcus Biel](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/1.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/2.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/3.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/4.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/5.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/6.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/7.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/8.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/9.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/10.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/11.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/12.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/13.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/14.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/15.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/16.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/17.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/18.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/19.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/20.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/21.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/22.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/23.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/24.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/25.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/26.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/27.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/28.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/29.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/30.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/31.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/32.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/33.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/34.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/35.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/36.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/37.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013013/587ad7b91a28ab542b8b481f/html5/thumbnails/38.jpg)
2016, Marcus Biel, http://www.marcus-biel.com/
Copyright © 2016
Marcus BielAll rights reserved
http://www.marcus-biel.com