{"id":20332,"date":"2022-11-09T10:00:39","date_gmt":"2022-11-09T04:30:39","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/"},"modified":"2022-11-09T10:00:39","modified_gmt":"2022-11-09T04:30:39","slug":"solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/","title":{"rendered":"[Solved] Default implementation for hashCode() and equals() for record vs class in Java"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-69333053\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"69333053\" data-parentid=\"69332957\" data-score=\"17\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>In a nutshell the difference is simple:<\/p>\n<ul>\n<li>the default implementation of <code>equals()<\/code> and <code>hashCode()<\/code> for <code>java.lang.Object<\/code> will never consider two objects as <code>equal<\/code> unless they are the same object (i.e. it&#8217;s &#8220;object identity&#8221;, i.e. <code>x == y<\/code>).<\/li>\n<li>the default implementation of <code>equals()<\/code> and <code>hashCode()<\/code> for records will consider all components (or fields) and compare them for equality (or consider them for the hash code). If they all match then <code>.equals()<\/code> will return <code>true<\/code> and <code>hashCode<\/code> will return the same values.<\/li>\n<\/ul>\n<p>The details documented <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/lang\/Object.html#hashCode()\">details for <code>java.lang.Object<\/code><\/a> are:<\/p>\n<blockquote>\n<p>As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object&#8217;s memory address at some point in time.)<\/p>\n<\/blockquote>\n<p>In practice this means that any object which doesn&#8217;t override <code>hashCode<\/code> anywhere in its type hierarchy will return what is called the &#8220;identity hash code&#8221; which is effectively an arbitrary but constant number.<\/p>\n<p>And for <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/17\/docs\/api\/java.base\/java\/lang\/Record.html#hashCode()\"><code>java.lang.Record<\/code> it says<\/a>:<\/p>\n<blockquote>\n<p>The implicitly provided implementation returns true if and only if the argument is an instance of the same record class as this record, and each component of this record is equal to the corresponding component of the argument; otherwise, false is returned. Equality of a component c is determined as follows:<\/p>\n<ul>\n<li>If the component is of a reference type, the component is considered equal if and only if Objects.equals(this.c, r.c) would return true.<\/li>\n<li>If the component is of a primitive type, using the corresponding primitive wrapper class PW (the corresponding wrapper class for int is java.lang.Integer, and so on), the component is considered equal if and only if PW.compare(this.c, r.c) would return 0.<\/li>\n<\/ul>\n<p>Apart from the semantics described above, the precise algorithm used in the implicitly provided implementation is unspecified and is subject to change. The implementation may or may not use calls to the particular methods listed, and may or may not perform comparisons in the order of component declaration.<\/p>\n<\/blockquote>\n<p>(Those are for the respective <code>hashCode<\/code> methods, the <code>equals<\/code> methods have similar language).<\/p>\n<p>For more discussion, see <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/openjdk.java.net\/jeps\/395\"><em>JEP 395: Records<\/em><\/a>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Default implementation for hashCode() and equals() for record vs class in Java <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] In a nutshell the difference is simple: the default implementation of equals() and hashCode() for java.lang.Object will never consider two objects as equal unless they are the same object (i.e. it&#8217;s &#8220;object identity&#8221;, i.e. x == y). the default implementation of equals() and hashCode() for records will consider all components (or fields) and compare &#8230; <a title=\"[Solved] Default implementation for hashCode() and equals() for record vs class in Java\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/\" aria-label=\"More on [Solved] Default implementation for hashCode() and equals() for record vs class in Java\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1815,2169,323,4760,4759],"class_list":["post-20332","post","type-post","status-publish","format-standard","hentry","category-solved","tag-equals","tag-hashcode","tag-java","tag-java-17","tag-java-record"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Default implementation for hashCode() and equals() for record vs class in Java - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Default implementation for hashCode() and equals() for record vs class in Java - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] In a nutshell the difference is simple: the default implementation of equals() and hashCode() for java.lang.Object will never consider two objects as equal unless they are the same object (i.e. it&#8217;s &#8220;object identity&#8221;, i.e. x == y). the default implementation of equals() and hashCode() for records will consider all components (or fields) and compare ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-09T04:30:39+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Default implementation for hashCode() and equals() for record vs class in Java\",\"datePublished\":\"2022-11-09T04:30:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/\"},\"wordCount\":371,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"equals\",\"hashcode\",\"java\",\"java-17\",\"java-record\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/\",\"name\":\"[Solved] Default implementation for hashCode() and equals() for record vs class in Java - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-09T04:30:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Default implementation for hashCode() and equals() for record vs class in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Default implementation for hashCode() and equals() for record vs class in Java - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Default implementation for hashCode() and equals() for record vs class in Java - JassWeb","og_description":"[ad_1] In a nutshell the difference is simple: the default implementation of equals() and hashCode() for java.lang.Object will never consider two objects as equal unless they are the same object (i.e. it&#8217;s &#8220;object identity&#8221;, i.e. x == y). the default implementation of equals() and hashCode() for records will consider all components (or fields) and compare ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/","og_site_name":"JassWeb","article_published_time":"2022-11-09T04:30:39+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Default implementation for hashCode() and equals() for record vs class in Java","datePublished":"2022-11-09T04:30:39+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/"},"wordCount":371,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["equals","hashcode","java","java-17","java-record"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/","url":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/","name":"[Solved] Default implementation for hashCode() and equals() for record vs class in Java - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-09T04:30:39+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-default-implementation-for-hashcode-and-equals-for-record-vs-class-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Default implementation for hashCode() and equals() for record vs class in Java"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=20332"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20332\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=20332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=20332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=20332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}