filmov
tv
#58 Object Class equals toString hashcode in Java
Показать описание
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
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
Комментарии