tag:blogger.com,1999:blog-8712770457197348465.post3501560647225953964..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: Overriding equals() and hashCode() method in Java and Hibernatejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger43125tag:blogger.com,1999:blog-8712770457197348465.post-46869560930300879722022-04-08T11:40:17.971-07:002022-04-08T11:40:17.971-07:00*their, not there*their, not thereRomanovhttps://www.blogger.com/profile/04909080064090899753noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81771502368902761562017-01-14T02:51:28.146-08:002017-01-14T02:51:28.146-08:00@Override public int hashCode() { final int prime ...@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + id; result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); return result;<br /><br /><br />here why we used "final int prime = 31"..some one please explain Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44468495208233195582016-11-29T05:18:26.419-08:002016-11-29T05:18:26.419-08:00Hi again @Nirav Khandhedia,
I know what's the...Hi again @Nirav Khandhedia,<br /><br />I know what's the problem! Passing to BigDecimal String vs double gives different behavior!!<br /><br />Regards!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81693449423777182862016-11-28T09:52:13.810-08:002016-11-28T09:52:13.810-08:00Looks like Nirav Khandhedia is right, BigDecimal ...Looks like Nirav Khandhedia is right, BigDecimal code works on 8, and 7 as well https://ideone.com/QG9Gce . Any comment, or remove this info?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59087017784893561992016-06-04T22:58:08.501-07:002016-06-04T22:58:08.501-07:00Hi,
Regarding inconsistency of BigDecimal equals ...Hi,<br /><br />Regarding inconsistency of BigDecimal equals and compareTo method, I see it is behaving consistently, may be changed after Java 7?<br /><br /> BigDecimal bd1 = new BigDecimal(11.30);<br /> BigDecimal bd2 = new BigDecimal(11.300);<br /> <br /> System.out.println("bd1 = " + bd1);<br /> System.out.println("bd2 = " + bd2);<br /> <br /> System.out.println(&Nirav Khandhedianoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-86881415861544938892014-12-18T06:43:19.638-08:002014-12-18T06:43:19.638-08:00@Anonymous, As per equals and hashcode contract, e...@Anonymous, As per equals and hashcode contract, equals() method should return false if compared with null and also if two object are not of same type than how can they be equal ? By the way HashMap doesn't have that requirement as well, you might be talking about HashSet which doesn't allow duplicates and uses equals() method to check for duplicates. HashSet will allow one null and Sachinnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36598245886402592882014-05-18T22:28:19.173-07:002014-05-18T22:28:19.173-07:00To let you know there’s a handy utility class call...To let you know there’s a handy utility class called EqualsTester available as part of the GSBase JUnit extensions (http://gsbase.sf.net), which helps enormously in verifying that a given class has implemented the delicate contract for equals and hashCode properly, or not. For example, it can verify the contract as specified by java.lang.Object which states that if A.equals(B) is true then Gopinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-74674848290225335972014-01-07T14:34:52.868-08:002014-01-07T14:34:52.868-08:00Sorry to say, but you are presenting a too complex...Sorry to say, but you are presenting a too complex set of ad-hoc rules, that inevitable will be forgotten, misunderstood, violated and/or abused by different developers in your team, and result in occasional bugs -- I promise you :)<br /><br />The first principle when defining equals/hashCode for ORM-entities, is to NEVER consider any other attributes than the ID (or UUIDs; see below) keys.<br />ropnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-49225136079957720812013-12-29T17:20:38.390-08:002013-12-29T17:20:38.390-08:00Can you please add info about use of equals and ha...Can you please add info about use of equals and hashcode in Hibernate. I mean why it's important to override equals and hashcode for Hiberante entity class, what happens if we don't? and how we can automate this process.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11784563799889114542013-09-17T06:37:48.203-07:002013-09-17T06:37:48.203-07:00I always initialize objects. That way there are n...I always initialize objects. That way there are no nulls. <br />public class Person {<br /> private int id = 0;<br /> private String firstName = "";<br /> private String lastName = "";<br /><br />I consider that the NullPointerException is a FLAW in the Java design. For example, 0 != "" != null - zero is not equal to empty string is not equal to null. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26906478637565639442013-09-17T06:28:25.249-07:002013-09-17T06:28:25.249-07:00This is why Java is 100 time more difficult than a...This is why Java is 100 time more difficult than any other language. In COBOL, you say IF FIELDA = FIELDB. Done. <br /><br />A word count on this subject:<br />Java - 3900 words<br />COBOL - six words. <br /><br />The winner? COBOL of course. At least, when you consider that progress means making things simpler, not more complex. <br /><br /> Baruch Attahttp://baruchatta.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4986105791748127142013-08-26T20:07:59.475-07:002013-08-26T20:07:59.475-07:00One of the better example, where equals method is ...One of the better example, where equals method is not consistent with compareTo is java.math.BigDecimal class. If you compare two BigDecimal object e.g. 120.00 and 120.000, equals method will return false, while compareTo will return zero. Both are inconsistent, because equals take both scale and value in consideration, while compareTo method only consider values.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81304708578048965622013-08-26T20:05:23.263-07:002013-08-26T20:05:23.263-07:00@Krishna, prime 31 is used to create hashCode whic...@Krishna, prime 31 is used to create hashCode which is uniformly distributed to avoid collisions in hash based collection classes. Inclusion of prime e.g. 31, 37 or 17 results in more uniformly distributed hashcode.Stuartnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45980286344222170642013-08-22T02:55:29.490-07:002013-08-22T02:55:29.490-07:00Can any one tel me y prime 31 used above...Can any one tel me y prime 31 used above...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69528251670919310742013-05-09T04:54:56.889-07:002013-05-09T04:54:56.889-07:00Hi, @Javin, I completely understood you. Of course...Hi, @Javin, I completely understood you. Of course, I forgot that if a variable references to null, none of its methods can be called. Thank you!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-49081910015061595582013-05-08T06:12:17.780-07:002013-05-08T06:12:17.780-07:00Hi @Maxim, this can not be null mate, because if t...Hi @Maxim, this can not be null mate, because if this would have been null your call to equals method has thrown NullPointerException already, since you are able to reach inside equals() means this is not null. Let me know if you disagree, and see any edge case, which is worth consideringJavin @ print array javahttp://javarevisited.blogspot.com/2012/12/3-example-to-print-array-values-in-java.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16258251685074011832013-05-06T05:52:09.013-07:002013-05-06T05:52:09.013-07:00Hello, why didn't you take care of this object...Hello, why didn't you take care of this object? If it is null, a NullPointerException will be thrown.<br /><br />if (this == null || obj == null || obj.getClass() != this.getClass()) {<br /> return false;<br />}<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-22361888120292876732013-03-31T22:06:41.699-07:002013-03-31T22:06:41.699-07:00@Override annotation on hashCode() method also pre...@Override annotation on hashCode() method also prevents subtle mistakes over return type. e.g. return type of hashCode method is int, but many times I tried to put long. If you are using IDE like netbeans, it will highlight that this is not overriding actual hashCode method.Gopinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6623487779426915412013-01-02T18:58:31.188-08:002013-01-02T18:58:31.188-08:00In one of Interview, one questions appeared How eq...In one of Interview, one questions appeared How equals method works in Java and explains this with example of String class's equals() method. I explained them that equals() is like any other method but called by many Java builtin classes e.g. Collections and you need to override hashcode when you implement equals method, is that correct ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36466408763251729942012-11-14T13:13:50.762-08:002012-11-14T13:13:50.762-08:00Thank you, thank you, THANK YOU for this informati...Thank you, thank you, THANK YOU for this informative tutorial! I am taking a programming languages class and am pretty much expected to already know Java, which I don't. This is incredibly helpful! Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-60338237130159840492012-07-29T18:43:02.173-07:002012-07-29T18:43:02.173-07:00Best tutorial on equals and hashCode I have read s...Best tutorial on equals and hashCode I have read so far. equals() and hashCode() example are very clear to me. Just to add :<br /><br />1) Two object which is logically equal but loaded from different ClassLoader can not be equals.<br /><br />2) Use EqulasBuilder and HashCodeBuilder from Apache commons for overriding equals and hashCode.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15662532160726012802012-07-29T14:01:02.309-07:002012-07-29T14:01:02.309-07:00Useful 4 test helper classes for equals hashcode ...Useful 4 test helper classes for equals hashcode are here https://github.com/chas66/jUnit-Paramterized-test<br />For junit3 can use the junit-Addons project on sourceforgeAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59235494956296578892012-07-29T06:39:14.712-07:002012-07-29T06:39:14.712-07:00Java 7 users can use
java.util.Objects.equals(fir...Java 7 users can use<br /><br />java.util.Objects.equals(firstName, guest.firstName)<br /><br />and<br /><br />Objects.hashCode(firstName)<br /><br />(nulls are handled automatically :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32830481661363649082012-07-27T04:14:13.384-07:002012-07-27T04:14:13.384-07:00Hi Saurabh, thanks for pointing that, Its indeed s...Hi Saurabh, thanks for pointing that, Its indeed short circuit AND operator && so that if first condition is not true don't all equals() method to avoid NullPointerExceptionJavin Paul @ sort arraylist in Javahttp://javarevisited.blogspot.com/2012/01/how-to-sort-arraylist-in-java-example.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78501999223143370362012-07-26T23:21:59.766-07:002012-07-26T23:21:59.766-07:00Under Common Errors while overriding equals in Jav...Under Common Errors while overriding equals in Java in Point 2 ; the code should make use of '&&' instead of '&'.So it should read :<br /><br />firstname == guest.firstname || (firstname != null && firstname.equals(guest.firstname)));<br /><br />instead of<br /><br />firstname == guest.firstname || (firstname != null & firstname.equals(guest.firstname)));Sourabhhttps://www.blogger.com/profile/09281834680203933704noreply@blogger.com