Which of the following options is the correct signature of the hashcode() method
Learn about Java 0. Show
1 and Table of Contents: 1. Uses of hashCode() and equals() Methods 2. Override the default behavior 3. EqualsBuilder and HashCodeBuilder 4. Generate hashCode() and equals() using Eclipse 5. Important things to remember 6. Special Attention When Using in ORM 1. Uses of hashCode() and equals() Methods
1.1. Contract between hashCode() and equals()Overriding the the
2. Overriding the Default BehaviorEverything works fine until we do not override any of both methods in our classes. But, sometimes, the application needs to change the default behavior of some objects. Let us understand why we need to override equals and hashcode methods. 2.1. The default behavior of Employee classLet’s take an example where your application has 5 object. Let us create a minimal possible structure of 5 class:
Above 5 class has some fundamental attributes and their accessor methods. Now consider a simple situation where you need to compare two Employee objects. Both employee objects have the same 8.
No prize for guessing. The above method will print “false.” But is it correct after knowing that both objects represent the same employee? In a real-time application, this should return 9.2.2. Should we override only equals() method?To achieve correct application behavior, we need to override
Add this method to the 5 class, and 2 will start returning 3.So are we done? Not yet. Let’s test the above-modified 5 class again in a different way.
The above example prints two objects in the second print statement. If both employee objects have been equal, in a 5 which stores unique objects, there must be only one instance inside 6 because both objects refer to the same employee. What is it we are missing??2.3. Overriding hashCode() is necessaryWe are missing the second important method 5 class.
Once the above method is added in Employee class, the second statement starts printing only a single object in the second statement and thus validating the true equality of 1 and 2.3. EqualsBuilder and HashCodeBuilderApache commons provide two excellent utility classes HashCodeBuilder and EqualsBuilder for generating hash code and equals methods. We can use these classes in the following manner.
4. Generating hashCode() and equals() in Eclipse IDEMost editors provide common source code templates. For example, Eclipse IDE has an option to generate an excellent implementation of Generate hashCode() and equals() In Eclipse 5. Best Practices
6. Special Attention When Using in ORMIf you’re dealing with an ORM, make sure always to use getters and never use the field references in For example, In our 5 class if we use 2. It is very much possible that 8 field is lazy-loaded. So, in this case, id field inside the methods might be zero or 4, and thus resulting in incorrect behavior.But if uses 5, we can be sure even if the field is lazy-loaded, calling the field getter will populate the field first.If you feel I am missing something or wrong somewhere, please leave a comment. I will update this post again to help others. What is the signature of hashCode method?Syntax: Following is the declaration of hashCode() method: public int hashCode() public static int hashCode(int value)
Which of the following options is the correct signature of the hashCode () method in Mcq?Explanation: In Java, the return type of hashCode() method is an integer, as it returns a hash code value for the object. Hence, the correct answer is the option (b).
What does the hashCode () method?The hashCode() method is defined in Java Object class which computes the hash values of given input objects. It returns an integer whose value represents the hash value of the input object. The hashCode() method is used to generate the hash values of objects.
Which of the following statements are true about the hashCode () method?What two statements are true about properly overridden hashCode() and equals() methods? hashCode() doesn't have to be overridden if equals() is. equals() doesn't have to be overridden if hashCode() is. hashCode() can always return the same value, regardless of the object that invoked it.
|