#58 Object Class equals toString hashcode in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

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();

Mobile mb2=new Mobile();




//use of equals() method to compare to object

//use of hashCode()
}
}

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 + price;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
return false;
Mobile other = (Mobile) obj;
if (model == null) {
return false;
return false;
return false;
return true;
}

}

class Main{
public static void main(String []args){
Mobile mb1=new Mobile();

Mobile mb2=new Mobile();

//use of toString() method, overrides method

//use of equals() method to compare two object, overrides method

//use of hashCode()

}
}

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.

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

🎯 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

nitishkumar-ktnb
Автор

I swear your are the only channel I see that explains everything perfectly.

zemariamkiros
Автор

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!!

wallshider
Автор

very well explained sir and the example of Laptop class is also very good. thanks for providing this for free❤❤

zakirdeshmukh
Автор

Thanks so much, I was really struggling. New subscriber unlocked🔓

missionimpossible
Автор

Sir your teaching style is too good!! It's very easy to grasp the concepts.

shreyamkundu
Автор

I saw yoour previous video and after seen this video I got more clearity. Thank you Sir

souvikadhikary
Автор

Thank you so much!!. Only because of you I am this able to hold a good package!

rimakumari-jn
Автор

Thank you! I was struggling to do a toString()

Ryu.
Автор

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.

Sartaj_Ashraf
Автор

This is old way now we have lombok library it's easyiest way to implement make a video for that.
Thank you

SamA-nqcf
Автор

Thank you sir. Amazing video as always

rishabhagarwal
Автор

what is hashcode for? why do we use the equals method. like why modify it?

merrylia
Автор

Super sir, i am a big fan of your work.

mohammadsulthan
Автор

how can we open the predefined class file (eg: Object.class) in vs code ?

gideonsamuel
Автор

Good video. Thank you for understanding this.

robsoonz
Автор

Awesome video but you didn't really explain the relationship between hashcode and equals

afifkhaja
Автор

Thank you for that explanation! So easy to follow and understand.

aileenchan
Автор

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 ?

halimgiwa
Автор

Hello sir I don't under stand 2 " . " here please explain

ghosts