Тёмный

Overriding equals and hashCode in Java - 039 

Deege
Подписаться 26 тыс.
Просмотров 82 тыс.
50% 1

How can I tell if two Java instances represent the same instance? And what does class equality mean in Java?
The question here is, what does it mean to be the same or different in Java. If you modeled me, maybe two of me had blue shirts and the third had a red shirt. So their shirt color attribute would be the blue for two DJ instances, and red for a third DJ instance. Are they equal because they have the same color shirt, or are they the same me? We're going to clear that up in this lesson as we learn about what it means in Java for two instances to be equal.
#java #programming #tutorial

Like the video? Don't forget to subscribe! ➜ www.deegeu.com/subscribe

Keep up to date with the newsletter! ➜ www.deegeu.com/newsletter/
Full transcript:
www.deegeu.com/overriding-equa...
Concepts: Java, classes, equals, hashcode
Social Links: Don't hesitate to contact me if you have any further questions.
WEBSITE : dj@deegeu.com
TWITTER : / deege
FACEBOOK: / deegeu.programming.tut...
GOOGLE+ : google.com/+Deegeu-programming...
Hosted by DJ Spiess
About Me: www.deegeu.com/about-programmi...
Related Videos:
What are static factory methods in Java? - 036
• What are static factor...
Free Java Course Online
• What important Java Te...
Media credits: All images are owned by DJ Spiess unless listed below:
Music
Cold Funk - Funkorama by Kevin MacLeod is licensed under a Creative Commons Attribution license (creativecommons.org/licenses/...)
Source: incompetech.com/music/royalty-...
Artist: incompetech.com/

Опубликовано:

 

21 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 98   
@jonesmm3
@jonesmm3 7 лет назад
"Equality is something that should be easy but has many nuances." -DJ 2015 Wisdom for the ages...
@Zydico
@Zydico 7 лет назад
Incredibly helpful, with very straightforward explanations!
@catzrulez5233
@catzrulez5233 6 лет назад
Nice visuals and editing, and more importantly good and concise teaching too. Thank you so much for sharing.
@ri5hipat
@ri5hipat 4 года назад
Your approach to teach stuff is unique, Thanks for this video!
@Allalilacias
@Allalilacias 5 месяцев назад
Thank you for the speed and clarity of the explanations. Great video!
@ngndnd
@ngndnd Год назад
finally someone who explains this. Every video i see just explains the basics
@sanjup100
@sanjup100 8 лет назад
Really nice way. I wasted two hours to understand this. Finally got it. Thanks Deege U
@deegeu
@deegeu 8 лет назад
+Arvind Pant Glad it helped!
@delucabruno
@delucabruno 8 лет назад
I just discovered your videos and I love them pleaase keep doing them It helps a lot! gratings from Argentina!
@deegeu
@deegeu 8 лет назад
+Bruno De Luca Thank you! Thanks for watching.
@Triviorito
@Triviorito 7 лет назад
Hey thanks for making this topic so fun to understand! Thumbs up!
@yogeshadsare3150
@yogeshadsare3150 7 лет назад
Thanks Deege U , the way of teaching is very good and effective. :)
@Utshaw
@Utshaw 7 лет назад
Hi, 5:22 you showed us a chart for creating overriden hashCode() method . Is it conventional / necessary to use that chart ?(Is it Oracle specific)
@zombiezombie1977
@zombiezombie1977 6 лет назад
Just found this channel...great job thanx :)
@dateneyoutubekanaal3667
@dateneyoutubekanaal3667 7 лет назад
Thanks, really helped me out, great video!
@proalexv
@proalexv Год назад
Thank you so much, great video would recommend that when talking about the overriding you noted when as many for the attributes, would be helpful if you're example included multiple to see how it would be extended. Either way, great video!
@mertyertugrul
@mertyertugrul 5 лет назад
Your tutorials are great, nice work.
@JD-kx1oe
@JD-kx1oe 7 лет назад
Very good explanation and funny as well. Good Job!
@mr_karamazov246
@mr_karamazov246 7 лет назад
Genuinely good video.
@terran008
@terran008 7 лет назад
wow this was a great explanation! thanks a lot for this video
@simplet3306
@simplet3306 4 года назад
The equal equal sign checks for value in the memory address of two objects not their fields that’s why we should override equals and hashcode. True or not?
@jennab8837
@jennab8837 6 лет назад
This video saved my grades. Thank you!
@user-xb7rh1sd4f
@user-xb7rh1sd4f Год назад
Great examples. Thank U
@yasminfatahi6361
@yasminfatahi6361 3 года назад
Great explanation, THANK YOU!
@Michael10145
@Michael10145 2 года назад
Best explanation I've seen. Cheers
@sayemsss560
@sayemsss560 6 лет назад
so much quality video. thank you !!!!
@amarsingh-cd8wn
@amarsingh-cd8wn 7 лет назад
greatly explained ... u rockk>>
@computerscientologist
@computerscientologist 6 лет назад
so helpful, thank you!
@keithscull8288
@keithscull8288 4 года назад
Great video THANK you! Been trying to understand why the Oracle documentation kept saying that if equals returned true hashcode must return true, what if I have 2 different objects with the same shirt color then that wouldnt be the case but what the docs fail to say in detail is that it's a conventional "must" not a technical default, n that you should override hashcode when overriding equals
@dhanatontanahal557
@dhanatontanahal557 7 лет назад
Sir what does the code inside hashcode mean? why we started with 7 and then with some formula? what exactly is the idea? thank you
@Joni67sinix
@Joni67sinix 5 лет назад
excellent video!! congratss and thanks!!
@Ztenam976
@Ztenam976 6 лет назад
You are a good explanator
@ErwinCorzo
@ErwinCorzo 7 лет назад
Te ganastes un suscriptor mas, Saludos!!!.
@deegeu
@deegeu 7 лет назад
Muchas gracias!
@Ravikumar-gj6qw
@Ravikumar-gj6qw 4 года назад
Nice and good explanation
@smugless191
@smugless191 3 года назад
Where did you get the table at 5:30 from?
@Martin-wx5ch
@Martin-wx5ch 3 года назад
Nicely explained
@muhammadhamedkamal1716
@muhammadhamedkamal1716 2 года назад
Thanks for explanation...
@_sudipidus_
@_sudipidus_ 6 лет назад
Informative short and succinct
@aneukmie
@aneukmie 8 лет назад
Great video! But where did you get the hash chart formula? Is it available somewhere in java documentation?
@deegeu
@deegeu 8 лет назад
+Mi Husna Krung I think I got it from the Effective Java book, but it's a standard strategy for creating hash codes. Most will have some form of this, but the prime number varies.
@anaskala1196
@anaskala1196 6 лет назад
could you possibly explain the meaning of (this==obj) in the overriding Methode code of equals. does it refer to obj1 in the call: obj1.equals(obj)??. if this is true, how does this work???. Thank you in advance.
@Buuhuuda
@Buuhuuda 3 года назад
Thank you for explaining something in 6 Minutes while my Professor needs 2 hours to overcomplicate this topic.
@muresanmarius7746
@muresanmarius7746 6 лет назад
I like how you make you're videos:)
@Unbreak00
@Unbreak00 7 лет назад
very nice, thank you.
@mayankparihar1988
@mayankparihar1988 8 лет назад
very great video.. I understand what you teach. but I have small question. why we using this process to generate hash.. what is reason behind it.
@deegeu
@deegeu 8 лет назад
+Mayank Parihar The hashcode provides a mostly unique number for your class instance of any size. You can think of it as a fingerprint of your class instance. It's not 100% unique, but most of the time it is. It's a quick way to determine if two instances are equal. If the hash is not equal, the instances are not equal. If the hashs are equal, it's likely the instances are equal.
@TheGazanews
@TheGazanews 8 лет назад
funny and effective thank you
@kshitijgupta6976
@kshitijgupta6976 4 года назад
very good video
@maximusdeisadze5744
@maximusdeisadze5744 3 года назад
why type cast "DJ" to obj if it passes the if statement?
@budsyremo
@budsyremo 6 лет назад
Don't 41*hash + myObject.hashCode() create hashcode which is diff for all ? I mean two shirts o same color always produce diff hashcode ?
@Kirmo13
@Kirmo13 Год назад
3:47 Why do you need to cast the instance if you just made sure that it's part of the DJ class?
@amyjen6934
@amyjen6934 6 месяцев назад
So that you can compare the specific parameters or use methods of that specific class. If you don't cast the Object parameter to the specific type of your class, you would be limited to using methods and fields defined in the Object class itself.
@GurleenKaur-ty4iy
@GurleenKaur-ty4iy 5 лет назад
You said equals method just calls the equal operator (==) , then how come we have different results for both in case of string?
@jvsnyc
@jvsnyc 3 года назад
The default inherited from Object class just calls the == operator. Most classes you will find override it to something more appropriate, including String.
@ameyapatil1139
@ameyapatil1139 6 лет назад
superb keep up
@HungryBirdStephenHaokip
@HungryBirdStephenHaokip 5 лет назад
Why start with prime non zero number .... plis some body explain me
@gautamprusty5336
@gautamprusty5336 8 лет назад
Hi Deeg U..I can't understand which circumstance we have to use hashcode and euals method ..If u give me answer ,It will make happy......
@deegeu
@deegeu 7 лет назад
Equals is when ever we want to determine two object values are equal. You could create two instances with the same classs values, but if you compare the references they will be different. One use of hashcode is when you want to store the values in a hashtable. If you don't provide equals and hashcode, you will get collisions in your hashtable.
@rahulmangulkar298
@rahulmangulkar298 4 года назад
To generate hashCode Eclipse is using one prime number where as Netbeans is using two prime numbers. Which one is correct? Below code is auto generated using Eclipse IDE. @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((shirtColor == null) ? 0 : shirtColor.hashCode()); return result; } Note: 1 is not prime number.
@MouradBENKADOUR
@MouradBENKADOUR 8 лет назад
Great job thanks, but what's the difference if I use in equals() method (this.getClass()==obj.getClass()) instead of (this instanceOf ObjClass), for me it's equal
@deegeu
@deegeu 8 лет назад
+Mourad BENKADOUR Good question! Testing getClass tests if the two types are identical. InstanceOf tests if the type on the left is an instance of the type or subtype of the type on the right.
@MouradBENKADOUR
@MouradBENKADOUR 8 лет назад
+Deege U ok thanks
@h3w45
@h3w45 5 лет назад
Thnk you.
@TheGazanews
@TheGazanews 8 лет назад
can you please cover more topics on java . this s the best way to lean period thank you a lot
@deegeu
@deegeu 8 лет назад
+mohamed elghannay I'll be doing Java for a long time. There's still many topics on just Core Java, and I'll hopefully get back to doing more Enterprise Java stuff soon!
@TheGazanews
@TheGazanews 8 лет назад
thanks
@punerealestatebuilder
@punerealestatebuilder 2 года назад
Amazed why this video has few likes even after 5 years. Great video and great style to explain.
@glueee2621
@glueee2621 8 лет назад
*Stupid question time By every instance is equal to each other, does that mean the traditional == way in which both instances are pointing towards the same object?
@deegeu
@deegeu 8 лет назад
If you use ==, you are testing if the two variables point to the same instance. If you use equals(), that tests if the values contained by the two instances are the same.
@louaykhammar7268
@louaykhammar7268 4 года назад
thanks
@egecant
@egecant 5 лет назад
YOU ARE AWESOMEEEEEEE
@KUROYUKI48
@KUROYUKI48 7 лет назад
is it possible to have multiple overriding equals and hashCode in Java?
@deegeu
@deegeu 7 лет назад
You should override it in each subclass. If you mean overloading, then no.
@KUROYUKI48
@KUROYUKI48 7 лет назад
Deege U thanks
@junaidmuhammad4091
@junaidmuhammad4091 7 лет назад
he said equal instances always produce the same hascode but i cant get why equal hashcodes do not mean equal instances. Any body can explain please?
@deegeu
@deegeu 7 лет назад
The hashcode is the output of a function that is usually unique. It's possible to have the same output for two different class instances. Think of it this way. There are a finite number of possible hashcodes since it's contained by an integer. There are more possible strings than integers, but all strings have hashcodes. This means some of the hashcodes are repeated for different strings. So having the same hashcode does not guarantee uniqueness.
@junaidmuhammad4091
@junaidmuhammad4091 7 лет назад
Deege U thank you so much you helped me alot. Keep it up!
@adriansalabanzi2250
@adriansalabanzi2250 5 лет назад
Like it!
@pritamkumarbehera5423
@pritamkumarbehera5423 3 года назад
Ohh... it's still helpful after 6years also
@fayyu5506
@fayyu5506 5 лет назад
👌
@gautamprusty5336
@gautamprusty5336 7 лет назад
Thanks,,,,,,,,,,,,,,,'
@pinological
@pinological 3 года назад
2:59
@supremereseller5953
@supremereseller5953 4 года назад
2020 gang, thank you
@rebeccabecky2229
@rebeccabecky2229 5 лет назад
Why a non zero prime number
@SreenathN92
@SreenathN92 7 лет назад
Why "DJ is cool" == "DJ is cool" is true? What does the compiler do?
@deegeu
@deegeu 7 лет назад
It's because Java interns the strings. So the two strings really do point to the same instance. This tutorial might help make it clearer. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-mS8Q8tApVnU.html
@Dante3085
@Dante3085 7 лет назад
Same memory address.
@Dante3085
@Dante3085 7 лет назад
Do "System.out.println(object1.toString());" "System.out.println(object2.toString());" for both objects. "toString()" gives you the memory adress of you'r object if it's not overwritten. So it should be the same for both objects.
@amarsingh-cd8wn
@amarsingh-cd8wn 7 лет назад
String string1=new String("java is cool"); String tring2=new String("java is cool"); if(string1==tring2) System.out.println("true"); else System.out.println("false"); now the output will be false becouse isolated memory is allocated for both string objects
@Dante3085
@Dante3085 7 лет назад
Yeah, different memory adresses, right?
@quickmaths4762
@quickmaths4762 3 года назад
what do you mean by "once we have done this for every attribute in our class"? No idea what you mean. Write another function for every attribute? Add Objects.hashCode(this.attr) for every attribute we have to the fourth line in your code? or even sth else?
@I3uzzzzzz
@I3uzzzzzz 4 года назад
yourHead.contains(hair)==false
@gautamprusty5336
@gautamprusty5336 7 лет назад
Hi Deeg, can u tell me ,why we are overriding equals and hashcode methods in our class,,,urgent required
@robinwenzel7743
@robinwenzel7743 5 лет назад
Triggered
@Ravikumar-gj6qw
@Ravikumar-gj6qw 3 года назад
Not explained clearly
Далее
Java Interfaces Explained - 040
7:12
Просмотров 155 тыс.
What it feels like cleaning up after a toddler.
00:40
.equals() vs. == in Java - The Real Difference
8:48
Просмотров 182 тыс.
Java. Методы equals и hashCode.
15:05
Просмотров 37 тыс.
I Melted Wood With Friction
8:44
Просмотров 705 тыс.
★ What are microservices?
5:39
Просмотров 361 тыс.
Java - Compare the Objects using equals() & hashCode()
16:16
When to use an abstract class in Java - 038
4:26
Просмотров 124 тыс.
Hands-on Java - Override hashCode and equals - Day 20
17:03