All language subtitles for 005 The equals() method_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

We can use the equals method to check if two objects are equal.

In this lesson, you're going to add an equals method to a class.

You can access this lesson by following this path and the resources and open the equals folder.

The default equals method compares the references of two objects.

Create one object of the car class.

Toyota blew.

And then set a variable, same car equal to the car.

Now, both car and same car share a reference to the same object.

Check if CA equals same car.

And it returns true because both variables store the same reference.

When you set same car equal to car, it copies the reference, and since both share the same reference,

then the equals method determines that they are indeed equal.

But that's the thing, too, variables should not share a reference to the same object.

Otherwise, you're going to risk falling into the reference trap.

So we're going to see that same car equal to a new copy of the first car object.

The two objects are exact copies of each other, but because they don't share the same reference than

the equals method determines that they're not equal.

We can customize the equals method for a specific class.

We can customize it to compare the fields of two objects, we're going to customize the equals method

for objects of the car class.

And as you can see, the equals method returns a boolean and it receives an object, what's the type

of that object?

We don't know.

It can be any object.

OK, car calls the equals method and pass the same cars in argument.

This points to the current object calling the equals method.

OBJ points to the object that was passed in.

And nothing happens inside the function, it just returns true.

That's not very useful.

Here is how to make the perfect equals method.

First, you're going to return false if the object that was passed then is null.

Then you're going to return false if the object is not an instance of the specified type.

If it happens to be an instance of the specified type, then you're going to typecast it.

Then you're going to compare every field from both objects and return the results.

In the equals method check if the object was passed in, is No.

And if that's the case, return false, there's no way the object that's calling the equals method is

going to equal null, otherwise it would have thrown a null pointer exception.

All right, now return false if the object that was passed in is not an instance of car and we can do

that using the instance of keyword, so we'll check if the object is an instance of car and reverse

it to say if the object is not of type car, then we'll return false.

Although if the object was passed in is an instance of car, then we are going to make it to this line

so we can typecast object to a car type.

And now we can access Hadfield's.

So what I'm going to do is compare every field from the current object calling the equals method, this

with every field from the car object that was passed in.

OK, time to test it inside, men, I'm going to pass a nail into the equals method.

This points to the current object and the argument we passed in is no to the equals method is going

to return false because there's no way a null is going to equal the object that's calling this method.

All right, now I'm going to create a scanner object.

And pass it into the equals method.

This points to the car object calling the method the object we passed in with scanner.

The object is not no to passes this test.

But the object is not an instance of car, so there's no way it's going to equal the car object that's

calling this method equals returns false and determines that these objects are not equal.

We'll try that out.

The object is not know, so it passes this test.

The object is of type car, so it passes that test.

The fields from both objects are equal to each other, so this condition returns true and the equals

method tells us that they are equal.

OK, now we're going to compare two car objects that don't equal each other, so I'll change this to

Honda.

All right, this object is not known.

So it passes this test.

The object is of type car, so it passes this test.

But the fields are equal to each other.

So this condition returns false and the equals method determines that these objects are not equal,

and you know what?

If you ask me, it works perfectly.

To recap, you customize the equals method for the car class.

The default equals method compares the references of two objects, which isn't very useful.

We customize the equals method to compare the fields from two car objects.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.