HashCode | Equals | toString Method in java Object Class

preview_player
Показать описание
Theory About HashCode method , Equals method , toString Method.

public native int hashCode() method:
 When you are creating any object, for all the objects hash code will be assigned internally by the JVM. On the basis of that hash code, JVM identifies each object uniquely. For each object there will be unique generation of hashCode() method is mainly responsible to generate the hashCode () and retunes in form of integer. According to requirements you can override hashCode method inside your class and provide your own implementation to generate the hashCode().

 If you want to change the hash code generation algorithm then you can override the hashCode() method inside your class with the following signature by using your own algorithm :-

SYNTAX:
public int hashCode()
{
own implementation
return…;
}

The hashCode() method implementation can be given in two different ways:-
i. Always returns the unique hashCode.
ii. Static implementation which returns same hashCode again and again.
It is always recommendable to write the hashCode() method implementation in a such way that return a unique hashCode for each object.

public boolean equals(Object) method:

a) On the basis of reference
b) On the basis of contents
 But if you want to compare two Objects on the basis of contents then override the equals(Object) method in the class with your own implementation with the following signature:

public boolean equals(Object o)
{
return…;
}

Contract between equals() method and hashCode() method :
Two equivalent Object should be placed in the same bucket but all the object available in the same bucket many not be equals also
1. Two equivalent Object :must have same hashCode() i.e
2. If two objects are not equal by equals(..)method then there is no restriction on their hashCode, may be same or may not be same.
3. If hashcode of two object are equals then these object may or may not equal by .equals method .
4. If hashCode of two object are not equal then these object are always not
equal by .equals method.

To satisfy above contract between equals() method and hashCode() method whenever we are overriding equals() method compulsory we have to override hashCode method otherwise we won’t get any compile time error or runtime error but it is not a good programming practice.

Also when to use throw and throws keyword with checked and unchecked Exception video explanation in java with example . Java Training Center (JTC) is specialized for java / j2ee Training only with Real Time Experts.

Java Training Center i.e JTC is professional software

training institute in noida and delhi, JTC Provides Practal

hands-on training , Summer Trainig , Corporate Training ,

College Campus Training, Customized Training According to

requirement with job assistance on the basis of freshers and

experience. JTC is also provides trained man-power/resource

on java , j2ee ,Spring ,Struts , hibernate ,XML , Webservices

,EJB.

JTC videos URL by Som Sir:

JTC Course Details by Som Sir

1. Online Core for beginners:

2.Online java class :- Object class Methods in java i.e

4.Core Java Basics classroom recording:

5.Exception Handling in Java

6.Multithreading in Java

7.Garbage Collection in Java

8.Object Oriented Concepts in java

9.Interview Question in java

10.String Class in Java

11. Servlet in java

12.JDBC in Java , java data base Connectivity in java

13.Enum in Java

14. Literals in Java

Contact JTC i.e Java training Center for any Corporate and

College Campus Training at: 9990399111 / 9990699111
Our Branches:
Noida : C-29, Sector-2,Noida
Greater Noida: Alpha-1st ,Kasana Tower, Greater Noida.
Рекомендации по теме
join shbcf.ru