All language subtitles for 003 Overriding hashCode()_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

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.