Back to Browse

HashCode | Equals | toString Method in java Object Class

21.0K views
Sep 27, 2017
37:56

In this java video tutorial I have discussed about java.lang.Object Methods in depth explanations. 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:  Equals() method which is available inside java.lang.Object class. Which is used to compare two objects in the following ways: a) On the basis of reference b) On the basis of contents  If you are using equals(Object) method default implementation which is available inside java.lang.Object class, then it will compare on the basis of reference.  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 h.equals (h2) is true the h1.hashCode()==h2.hashCode() should return true . 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. Mail us your requirements to- [email protected] Or 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 https://goo.gl/yVDmVX 1. Online Core for beginners: https://goo.gl/DTqhCa 2.Online java class :- Object class Methods in java i.e java.lang package https://goo.gl/RhPfwL 3.hashCode , toString , equals methods in Java Object class: https://goo.gl/6BrVQU 4.Core Java Basics classroom recording: https://goo.gl/aXXnJ3 5.Exception Handling in Java https://goo.gl/wTdJdL 6.Multithreading in Java https://goo.gl/jfu1Pw 7.Garbage Collection in Java https://goo.gl/88Eqw8 8.Object Oriented Concepts in java https://goo.gl/xj4cNd 9.Interview Question in java https://goo.gl/659Vi8 10.String Class in Java https://goo.gl/ikvvn1 11. Servlet in java https://goo.gl/xN5CyY 12.JDBC in Java , java data base Connectivity in java https://goo.gl/dz3D6k 13.Enum in Java https://goo.gl/EEaMkN 14. Literals in Java https://goo.gl/DoV8jV Contact JTC i.e Java training Center for any Corporate and College Campus Training at: 9990399111 / 9990699111 Visit: www.jtcindia.org Our Branches: Noida : C-29, Sector-2,Noida Greater Noida: Alpha-1st ,Kasana Tower, Greater Noida.

Download

1 formats

Video Formats

360pmp456.7 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

HashCode | Equals | toString Method in java Object Class | NatokHD