Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
When you override equals, you need to always override harsh code.
Objects that are deemed equal must always share the same hash code.
So when you override equals, you need to always override a matching hash code method.
In this lesson, you're going to override the harsh code method.
As we continue this lesson, I really want you to keep this in mind, objects that are deemed equal
must always share the same hash code.
If you break this rule, you're going to get bugs.
OK, inside person, not Java, highlight the equals method and commented out press command slightly
for using a Mac or control for Windows.
And now inside Maine, we're going to set the two variables directly equal to each other.
First, we're going to test for equality.
And then we're going to print the hash code of each object.
OK, now we're on the debugger, both variables, a reference that points to the same object.
And the equals method that every class inherits by default, it compares the reference of each object,
so naturally it's going to return.
True.
And the default passcode method returns system DOT, identity hash code, this system, dot identity
hash code, it takes the object, this the current object and it returns a number that never changes
during that object's lifetime.
So because those two variables share a reference to the exact same object, then the hash code method
is going to return the same number twice.
OK, now we're going to set the variable equal to a new copy of the first object.
Run the debugger, each variable story is a unique reference that points to a new object.
The default equals method that every class inherits, it compares each reference there are not equals
or returns false.
And it seems the high school method returns a different hash code for each object.
OK, that makes sense.
Before we keep going, remember this rule, the equality contract states that objects deemed equal must
always share the same hash code.
So what happens when we override the equals method without overriding the hash code method?
Uncommented.
Set the two objects directly equal to each other.
Now run the debugger.
Both variables share a reference that points to the same object and the equals method that we override
returns true because well, obviously the two variables point to the exact same object.
So that's fine.
Now, Hoshko, to remember that system, that identity has code takes the current object that's calling
this method this, and it returns a number that never changes during that object's lifetime.
Both variables point to the exact same object.
So the hash code method is going to return the same number twice.
So far, we haven't broken this rule, we haven't broken the equality contract.
The objects are deemed equal and they share the same hash code.
Now, what happens if I set the variable equal to a new copy of the object?
Run the debugger, each variable store is a unique reference that points to a new object.
And our equals method compares the fields for each object.
But now the high skilled method, it gets, the current object that calls it and it returns a number
and identity has good, that never changes for the objects entire lifetime, and each variable points
to a different object.
So that gives one object a housecoat, a few for whatever.
It gives another object a harsh code of seven zero eight, et cetera, and that's not good.
We broke the rule.
We broke the equality contract.
Objects that are deemed equal must always share the same hash code, otherwise you're going to get a
lot of bugs, as you saw in the hash map part of this course.
So what's the solution when you override equals just always remember to override hash code.
If you do that, you're going to get peace of mind.
So the hash good method that we overrode, it needs to assign equal objects, the same hash code, we're
going to use the familiar objects that hash method.
And what that's going to do is assign equal objects, objects that have similar fields, the same house
code.
And inside, you have to add in your fields, of course.
And now if you rerun the debugger.
The two objects are equal according to the equals method.
Will step inside.
And it calls the harsh code method we overrode.
The method returns a number based on the objects fields.
The second object has the exact same field, so the hash function is going to return the same number.
And beautiful, now we're good, equal objects share the same housecoat, and that's how it should be.
Let's recap, you overrode the hash code method.
Objects that are deemed equal must also share the same hash code, so when you override equals always
remember to override hash code, otherwise you're going to get bugs.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.