Overriding equals and hashCode in Java - 039

  Рет қаралды 82,150

Deege

Deege

Күн бұрын

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/

Пікірлер: 98
@jonesmm3
@jonesmm3 7 жыл бұрын
"Equality is something that should be easy but has many nuances." -DJ 2015 Wisdom for the ages...
@ngndnd
@ngndnd Жыл бұрын
finally someone who explains this. Every video i see just explains the basics
@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.
@Allalilacias
@Allalilacias 5 ай бұрын
Thank you for the speed and clarity of the explanations. Great video!
@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!
@ri5hipat
@ri5hipat 4 жыл бұрын
Your approach to teach stuff is unique, Thanks for this video!
@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. :)
@dateneyoutubekanaal3667
@dateneyoutubekanaal3667 7 жыл бұрын
Thanks, really helped me out, great video!
@jennab8837
@jennab8837 6 жыл бұрын
This video saved my grades. Thank you!
@zombiezombie1977
@zombiezombie1977 6 жыл бұрын
Just found this channel...great job thanx :)
@mr_karamazov246
@mr_karamazov246 7 жыл бұрын
Genuinely good video.
@user-xb7rh1sd4f
@user-xb7rh1sd4f Жыл бұрын
Great examples. Thank U
@mertyertugrul
@mertyertugrul 5 жыл бұрын
Your tutorials are great, nice work.
@Buuhuuda
@Buuhuuda 3 жыл бұрын
Thank you for explaining something in 6 Minutes while my Professor needs 2 hours to overcomplicate this topic.
@JD-kx1oe
@JD-kx1oe 7 жыл бұрын
Very good explanation and funny as well. Good Job!
@Michael10145
@Michael10145 2 жыл бұрын
Best explanation I've seen. Cheers
@terran008
@terran008 7 жыл бұрын
wow this was a great explanation! thanks a lot for this 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!
@yasminfatahi6361
@yasminfatahi6361 3 жыл бұрын
Great explanation, THANK YOU!
@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?
@amarsingh-cd8wn
@amarsingh-cd8wn 7 жыл бұрын
greatly explained ... u rockk>>
@sayemsss560
@sayemsss560 6 жыл бұрын
so much quality video. thank you !!!!
@Ravikumar-gj6qw
@Ravikumar-gj6qw 4 жыл бұрын
Nice and good explanation
@computerscientologist
@computerscientologist 6 жыл бұрын
so helpful, thank you!
@Martin-wx5ch
@Martin-wx5ch 3 жыл бұрын
Nicely explained
@ErwinCorzo
@ErwinCorzo 7 жыл бұрын
Te ganastes un suscriptor mas, Saludos!!!.
@deegeu
@deegeu 7 жыл бұрын
Muchas gracias!
@muhammadhamedkamal1716
@muhammadhamedkamal1716 2 жыл бұрын
Thanks for explanation...
@Joni67sinix
@Joni67sinix 5 жыл бұрын
excellent video!! congratss and thanks!!
@punerealestatebuilder
@punerealestatebuilder 2 жыл бұрын
Amazed why this video has few likes even after 5 years. Great video and great style to explain.
@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)
@Ztenam976
@Ztenam976 6 жыл бұрын
You are a good explanator
@_sudipidus_
@_sudipidus_ 6 жыл бұрын
Informative short and succinct
@kshitijgupta6976
@kshitijgupta6976 4 жыл бұрын
very good video
@muresanmarius7746
@muresanmarius7746 6 жыл бұрын
I like how you make you're videos:)
@TheGazanews
@TheGazanews 8 жыл бұрын
funny and effective thank you
@ameyapatil1139
@ameyapatil1139 6 жыл бұрын
superb keep up
@Unbreak00
@Unbreak00 7 жыл бұрын
very nice, thank you.
@pritamkumarbehera5423
@pritamkumarbehera5423 3 жыл бұрын
Ohh... it's still helpful after 6years also
@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.
@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
@smugless191
@smugless191 3 жыл бұрын
Where did you get the table at 5:30 from?
@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
@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
@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.
@egecant
@egecant 5 жыл бұрын
YOU ARE AWESOMEEEEEEE
@louaykhammar7268
@louaykhammar7268 4 жыл бұрын
thanks
@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 ?
@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.
@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.
@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.
@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.
@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 8 жыл бұрын
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.
@maximusdeisadze5744
@maximusdeisadze5744 3 жыл бұрын
why type cast "DJ" to obj if it passes the if statement?
@adriansalabanzi2250
@adriansalabanzi2250 5 жыл бұрын
Like it!
@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
@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.
@gautamprusty5336
@gautamprusty5336 8 жыл бұрын
Thanks,,,,,,,,,,,,,,,'
@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
@fayyu5506
@fayyu5506 5 жыл бұрын
👌
@HungryBirdStephenHaokip
@HungryBirdStephenHaokip 5 жыл бұрын
Why start with prime non zero number .... plis some body explain me
@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!
@pinological
@pinological 3 жыл бұрын
2:59
@supremereseller5953
@supremereseller5953 4 жыл бұрын
2020 gang, thank you
@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?
@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. kzbin.info/www/bejne/o4SbgmuqdtWJpLc
@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?
@rebeccabecky2229
@rebeccabecky2229 5 жыл бұрын
Why a non zero prime number
@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 4 жыл бұрын
Not explained clearly
Java Interfaces Explained - 040
7:12
Deege
Рет қаралды 155 М.
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 106 МЛН
A clash of kindness and indifference #shorts
00:17
Fabiosa Best Lifehacks
Рет қаралды 123 МЛН
Secret Experiment Toothpaste Pt.4 😱 #shorts
00:35
Mr DegrEE
Рет қаралды 8 МЛН
Java. Методы equals и hashCode.
15:05
Sergey Arkhipov Java Tutorials
Рет қаралды 37 М.
The equals hashCode Contract - Java Programming
22:31
Will Tollefson
Рет қаралды 828
Java - Compare the Objects using equals() & hashCode()
16:16
Coding Simplified
Рет қаралды 44 М.
.equals() vs. == in Java - The Real Difference
8:48
Coding with John
Рет қаралды 182 М.
02. Equals and HashCode Contract & Different Variations
11:19
WebEncyclop Tutorials
Рет қаралды 53 М.
Hands-on Java - Override hashCode and equals - Day 20
17:03
Caleb Curry
Рет қаралды 4,3 М.
How to write a copy constructor in Java - 037
5:38
Deege
Рет қаралды 34 М.
Java generics ❓
22:04
Bro Code
Рет қаралды 104 М.
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 106 МЛН