Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: " 2016, Marcus Biel, Marcus Biel, Software Craftsman Identity vs Equality in Java"— Presentation transcript:

1  2016, Marcus Biel, http://www.marcus-biel.com/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/

2  2016, Marcus Biel, http://www.marcus-biel.com/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.

3  2016, Marcus Biel, http://www.marcus-biel.com/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.

4  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ Identity vs Equality To be able to locate an object, every object is assigned an address in the memory.

5  2016, Marcus Biel, http://www.marcus-biel.com/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”.

6  2016, Marcus Biel, http://www.marcus-biel.com/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”?

7  2016, Marcus Biel, http://www.marcus-biel.com/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.

8  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ Identity Operator a == b To check for object identity, you use the “equals“ operator.

9  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ Coding Example To illustrate this, let’s do a little coding example.

10  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ Coding Example Car myCar1 myCar1 We create a reference variable of type Car with name “myCar1”…

11  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ Coding Example Car myCar1 = new Car("blue"); myCar1 Car Object 1 …and assign it a Car Object.

12  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ Coding Example Car myCar1 = new Car("blue"); Car myCar2 myCar1 myCar2 Car Object 1 Now we create a second Car reference variable named “myCar2”…

13  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

14  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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”…

15  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

16  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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?

17  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

18  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

19  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

20  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

21  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

22  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

23  2016, Marcus Biel, http://www.marcus-biel.com/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.

24  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ Equality However, in other cases we just want to know – is this a white house?…

25  2016, Marcus Biel, http://www.marcus-biel.com/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.

26  2016, Marcus Biel, http://www.marcus-biel.com/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.

27  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

28  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

29  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

30  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

31  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

32  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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?

33  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

34  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ 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.

35  2016, Marcus Biel, http://www.marcus-biel.com/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.

36  2016, Marcus Biel, http://www.marcus-biel.com/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); } 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.

37  2016, Marcus Biel, http://www.marcus-biel.com/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); } 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.

38  2016, Marcus Biel, http://www.marcus-biel.com/http://www.marcus-biel.com/ Copyright © 2016 Marcus Biel All rights reserved http://www.marcus-biel.com


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

Similar presentations


Ads by Google