#58 Object Class equals toString hashcode in Java

  Рет қаралды 97,196

Telusko

Telusko

Жыл бұрын

Check out our courses:
Enterprise Java Spring Microservices: go.telusko.com/enterpriseJava
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Coupon: TELUSKO20 (20% Discount)
Udemy Courses:
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
Java For Programmers:- bit.ly/javaProgrammers
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
In this lecture we are discussing about object class:
-- every class in java inherit object class
-- in this lecture we see some member method of object class
public native int hashCode();
public boolean equals( Object);
public String toString();
1)hashCode() method:
In Java, the hashCode () method is a method that is defined in the Object class,
which is the parent class of all classes in Java. It returns an integer value that
represents the unique hash code of an object.
2)equals(Object) method:
equals(Object obj) is the method of Object class. This method is used to compare
the given objects. It is suggested to override equals(Object obj) method to get our own equality condition on Objects.
3)toString() method:
We typically generally do use the toString() method to get the string representation of an object. It is very important
and readers should be aware that whenever we try to print the object reference then internally toString() method is invoked.
If we did not define the toString() method in your class then the Object class toString() method is invoked otherwise our
implemented or overridden toString() method will be called.
case 1: class which not override object class toString(), hashCode(), equals() method
class Mobile{
String model;
int price;
}
class Main{
public static void main(String []args){
Mobile mb1=new Mobile();
mb1.model="Apple";
mb1.price=100000;
Mobile mb2=new Mobile();
mb2.model="Apple";
mb2.price=100000;
System.out.println(mb1); //Internally mb1.toString() is called and print Mobile@4617c264
System.out.println(mb2); // Internally mb2.toString() is called and print Mobile@36baf30c
//use of equals() method to compare to object
boolean result =mb1.equals(mb2); //right now it give false result because by default implementation of equals() method compare reference of two objects
System.out.println(result); //false
//use of hashCode()
System.out.println(mb1.hashCode()); //1175962212
System.out.println(mb2.hashCode()); //918221580 , provide some unique value
}
}
case 2: class can override object class hashCode(), toString(), equals()
class Mobile{
String model;
int price;
@Override
public String toString(){
return "Model: "+model+" and price: "+price;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((model == null) ? 0 : model.hashCode());
result = prime * result + price;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Mobile other = (Mobile) obj;
if (model == null) {
if (other.model != null)
return false;
} else if (!model.equals(other.model))
return false;
if (price != other.price)
return false;
return true;
}
}
class Main{
public static void main(String []args){
Mobile mb1=new Mobile();
mb1.model="Apple";
mb1.price=100000;
Mobile mb2=new Mobile();
mb2.model="Apple";
mb2.price=100000;
//use of toString() method, overrides method
System.out.println(mb1); //Internally mb1.toString() is called and print Model: Apple and price: 100000
System.out.println(mb2); // Internally mb2.toString() is called and print Model: Apple and price: 100000
//use of equals() method to compare two object, overrides method
boolean result =mb1.equals(mb2); //right now it give true result because we override equals() method
System.out.println(result); //true
//use of hashCode()
System.out.println(mb1.hashCode()); //1967873639 due to overrides hashcode method
System.out.println(mb2.hashCode()); //1967873639
System.out.println(mb1==mb2);
}
}
Note: it is not mandatory to override every member method of object class but it is advice able
to override toString() and equals() method to compare and print own object.
Github repo : github.com/navinreddy20/Javac...
Donation:
PayPal Id : navinreddy20
www.telusko.com

Пікірлер: 46
@nitishkumar-kt1nb
@nitishkumar-kt1nb 4 ай бұрын
🎯 Key Takeaways for quick navigation: Every class in Java implicitly extends the Object class. The Object class provides methods like equals, hashCode, toString, etc., even if not explicitly defined in a class. The toString method returns a string representation of the object, including the class name and hash code. The hashCode method generates a unique identifier for objects based on their values. Using equals method from the Object class compares objects based on their memory addresses, but custom equals method can compare based on values. Implementing custom equals and hashCode methods ensures proper comparison and adherence to Java best practices. IDEs can automatically generate equals and hashCode methods based on selected variables, simplifying implementation. Understanding and utilizing methods from the Object class such as toString and equals is essential in Java programming. Made with HARPA AI
@mukeshkannan4546
@mukeshkannan4546 20 сағат бұрын
Thanks buddy ❤
@wallshider
@wallshider 2 ай бұрын
Christ almighty.. I've been pulling my hair trying to understand chatgpt to explain to me why I need hashcode in operator == overriding and here you go, explaining everything! Thank you!!
@zemariamkiros5133
@zemariamkiros5133 2 ай бұрын
I swear your are the only channel I see that explains everything perfectly.
@zakirdeshmukh9916
@zakirdeshmukh9916 Жыл бұрын
very well explained sir and the example of Laptop class is also very good. thanks for providing this for free❤❤
@souvikadhikary2429
@souvikadhikary2429 Жыл бұрын
I saw yoour previous video and after seen this video I got more clearity. Thank you Sir
@shreyamkundu
@shreyamkundu 14 күн бұрын
Sir your teaching style is too good!! It's very easy to grasp the concepts.
@rishabhagarwal3404
@rishabhagarwal3404 Жыл бұрын
Thank you sir. Amazing video as always
@mohammadsulthan9379
@mohammadsulthan9379 Жыл бұрын
Super sir, i am a big fan of your work.
@robsoonz7766
@robsoonz7766 Жыл бұрын
Good video. Thank you for understanding this.
@jaredwilliams4994
@jaredwilliams4994 6 ай бұрын
Greatest Java Tutorial ever!!
@Ryu.86
@Ryu.86 28 күн бұрын
Thank you! I was struggling to do a toString()
@kavya4132
@kavya4132 4 ай бұрын
great explanation. Thank you!
@user-kk1ym2go6o
@user-kk1ym2go6o 2 ай бұрын
Thank you so much!!. Only because of you I am this able to hold a good package!
@28santagabo
@28santagabo 29 күн бұрын
great explanation sir
@aileenchan3741
@aileenchan3741 Жыл бұрын
Thank you for that explanation! So easy to follow and understand.
@SamA-nq4cf
@SamA-nq4cf 27 күн бұрын
This is old way now we have lombok library it's easyiest way to implement make a video for that. Thank you
@milankbudha
@milankbudha 11 ай бұрын
thank u so much....❤❤❤
@oshadhaedirisinghe1455
@oshadhaedirisinghe1455 3 ай бұрын
Thank you
@Sartaj_Ashraf
@Sartaj_Ashraf 9 ай бұрын
In the first == it should return false they are two different objects with two different locations.. So equall methods returns true if the reference are same means they lie on the same location.
@merrylia8316
@merrylia8316 9 ай бұрын
what is hashcode for? why do we use the equals method. like why modify it?
@pankajubhare1699
@pankajubhare1699 11 ай бұрын
You just awesome
@ghosts857
@ghosts857 Жыл бұрын
Hello sir I don't under stand 2 " . " here please explain
@sharabugnanesh3098
@sharabugnanesh3098 Жыл бұрын
model model 👌
@halimgiwa5359
@halimgiwa5359 Ай бұрын
In the toString() method, we simply used "model" and "price" but in the equals() method we had to use "this.model" and "this.price," why ?
@bhargav7136
@bhargav7136 11 ай бұрын
Why we use hash code
@farhodbekxamidov2013
@farhodbekxamidov2013 6 ай бұрын
👍
@koushikjatoth6000
@koushikjatoth6000 10 ай бұрын
Can I get tutorial ppt
@afifkhaja
@afifkhaja 5 ай бұрын
Awesome video but you didn't really explain the relationship between hashcode and equals
@kelvinirungu550
@kelvinirungu550 4 ай бұрын
The Object class is the relationship between hashcode and equals because every class in Java implicitly extends the Object class.
@brandonjablasone7544
@brandonjablasone7544 5 ай бұрын
What ide you use
@p.sabareesh2053
@p.sabareesh2053 13 күн бұрын
VS code
@MrRobo-fi7of
@MrRobo-fi7of 3 ай бұрын
@ghosts857
@ghosts857 Жыл бұрын
this.model. equal () that please explain
@hieuthanh2735
@hieuthanh2735 7 ай бұрын
"this" means the current object which calls the method
@thecrazygamer7334
@thecrazygamer7334 9 ай бұрын
chomu padhane ka dhnage nahi hai
@keshavdeosharma7222
@keshavdeosharma7222 5 ай бұрын
bhai apne channel ki link send krdo tb
@harshsinghania6775
@harshsinghania6775 7 ай бұрын
I came to learn the generated code... But here you generated and skipped the actual use of each and every line in the code... Disappointed alien..
@user-is8xs7zb7m
@user-is8xs7zb7m 4 ай бұрын
OMG! Do you know at least what you said? This is very sad for beginners
@SurendraGurjar2014
@SurendraGurjar2014 Жыл бұрын
Way of explaining is not good
@PriyaDharshini-gr5hk
@PriyaDharshini-gr5hk 11 ай бұрын
Why
@Psuedobot
@Psuedobot 10 ай бұрын
Just this one only
@hieuthanh2735
@hieuthanh2735 7 ай бұрын
If you want to understand more, you have to watch the previous video @@Psuedobot
@shubhammisal946
@shubhammisal946 7 ай бұрын
Right😢
@pubg_victor_gaming
@pubg_victor_gaming 6 ай бұрын
😂 acha mazaq hai
#59 Upcasting and Downcasting in Java
6:37
Telusko
Рет қаралды 89 М.
#53 Packages in Java
12:20
Telusko
Рет қаралды 168 М.
Зачем он туда залез?
00:25
Vlad Samokatchik
Рет қаралды 3,3 МЛН
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 181 МЛН
What it feels like cleaning up after a toddler.
00:40
Daniel LaBelle
Рет қаралды 77 МЛН
02. Equals and HashCode Contract & Different Variations
11:19
WebEncyclop Tutorials
Рет қаралды 53 М.
.equals() vs. == in Java - The Real Difference
8:48
Coding with John
Рет қаралды 182 М.
#61 Abstract Keyword in Java
12:09
Telusko
Рет қаралды 133 М.
#60 Wrapper Class in Java
8:08
Telusko
Рет қаралды 160 М.
Java toString method 🎉
6:39
Bro Code
Рет қаралды 90 М.
#57 Final keyword in java
6:15
Telusko
Рет қаралды 70 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1 МЛН
#71 What is Annotation in Java
7:32
Telusko
Рет қаралды 66 М.
КРУТОЙ ТЕЛЕФОН
0:16
KINO KAIF
Рет қаралды 6 МЛН
Looks very comfortable. #leddisplay #ledscreen #ledwall #eagerled
0:19
LED Screen Factory-EagerLED
Рет қаралды 2,8 МЛН
Telefonu Parçaladım!😱
0:16
Safak Novruz
Рет қаралды 25 МЛН
Копия iPhone с WildBerries
1:00
Wylsacom
Рет қаралды 7 МЛН