If you are not sure if the website you would like to visit is secure, you can verify it here. Enter the website address of the page and see parts of its content and the thumbnail images on this site. None (if any) dangerous scripts on the referenced page will be executed. Additionally, if the selected site contains subpages, you can verify it (review) in batches containing 5 pages.
favicon.ico: nipafx.dev/implement-java-equals-correctly - How To Implement equals Correc.

site address: nipafx.dev/implement-java-equals-correctly redirected to: nipafx.dev/implement-java-equals-correctly

site title: How To Implement equals Correctly // nipafx

Our opinion (on Monday 14 September 2026 22:50:16 UTC):

GREEN status (no comments) - no comments
After content analysis of this website we propose the following hashtags:


Hashtags existing on this website:



page from cache: 1 hour ago
Meta tags:
description=A fundamental aspect of any Java class is its definition of equality. It is determined by a class s equals method. Let s see how to implement it correctly.;
keywords=equals;

Headings (most frequently used words):

equals, check, equality, how, to, implement, correctly, identity, versus, thoughts, on, the, contract, implementing, summary, final, words, signature, self, null, type, and, cast, field, comparison,

Text of the page (most frequently used words):
the (75), #equals (73), and (59), java (51), person (43), that (35), this (33), equal (26), for (24), check (23), null (22), some (21), string (21), are (20), return (20), but (19), true (19), #equality (18), not (15), with (15), other (15), new (14), project (14), have (13), one (12), objects (12), object (12), same (12), firstname (12), lastname (12), any (12), they (12), class (11), implementation (11), can (11), reference (11), implement (10), all (10), which (10), comparison (10), there (10), should (10), field (10), employee (10), false (10), identical (10), what (9), our (9), how (9), use (9), make (9), getclass (9), method (9), code (9), let (8), more (8), non (8), then (8), because (8), elliot (8), boolean (8), foo (7), fields (7), identity (7), value (7), its (7), instances (7), type (7), out (7), would (7), example (7), classes (6), like (6), subtypes (6), instanceof (6), self (6), sure (6), called (6), correctly (6), also (6), about (6), things (6), hashcode (5), you (5), look (5), final (5), checked (5), checks (5), work (5), get (5), way (5), two (5), implementing (5), three (5), only (5), from (5), mrrobot (5), first (5), contract (5), junit (5), contains (4), collections (4), your (4), them (4), makes (4), words (4), compare (4), override (4), better (4), could (4), core (4), note (4), share (4), instance (4), above (4), well (4), might (4), returns (4), both (4), thing (4), cast (4), very (4), performance (4), call (4), signature (4), now (4), alderson (4), properties (4), defined (4), article (4), community (4), license (3), their (3), these (3), methods (3), allow (3), libfx (3), public (3), testing (3), simple (3), cases (3), always (3), different (3), references (3), summary (3), does (3), much (3), was (3), site (3), never (3), slice (3), without (3), transitivity (3), third (3), fuu (3), whether (3), compares (3), say (3), turn (3), unexpected (3), next (3), another (3), here (3), fundamental (3), many (3), values (3), has (3), right (3), definition (3), thoughts (3), point (3), variable (3), list (3), variables (3), versus (3), follow (3), post (3), basics (3), pioneer (3), demos (3), streams (3), clean (3), privacy (2), rss (2), stackoverflow (2), github (2), twitch (2), youtube (2), discord (2), mastodon (2), bluesky (2), using (2), implementations (2), needs (2), verify (2), equalsverifier (2), test (2), will (2), consider (2), where (2), own (2), include (2), those (2), must (2), exactly (2), already (2), uses (2), obvious (2), fact (2), extra (2), sense (2), really (2), problems (2), inherited (2), occurs (2), end (2), apparently (2), quite (2), trivial (2), posts (2), details (2), reflexivity (2), obviously (2), included (2), adds (2), clearly (2), extends (2), may (2), behavior (2), maybe (2), framework (2), runtime (2), means (2), had (2), even (2), actually (2), property (2), being (2), collection (2), thus (2), just (2), most (2), declared (2), otherwise (2), assume (2), through (2), data (2), structures (2), consistently (2), equivalence (2), relation (2), source (2), leave (2), longer (2), laptop (2), yours (2), second (2), strings (2), aspect (2), determined (2), couple (2), considered (2), correct (2), active (2), various (2), platforms (2), watch (2), space (2), notified (2), when (2), publish (2), content (2), originally (2), wrote (2), sitepoint (2), channel (2), talks (2), record (2), args (2), var (2), year (2), tools (2), techniques (2), switch (2), serialization (2), reflection (2), records (2), valhalla (2), panama (2), loom (2), leyden (2), amber (2), patterns (2), pattern (2), matching (2), optional (2), ramp (2), migration (2), meta (2), maven (2), libraries (2), lambda (2), javafx (2), j_ms (2), generics (2), dop (2), documentation (2), deprecation (2), libs (2), architecture (2), tags (2), works (2), javascript (2), contact, imprint, control, over, suit, plain, wrong, rescue, features, user, specify, she, transforming, forclass, equalscontract, void, seen, properly, soon, upheld, rules, liner, ide, generate, edit, needed, desired, across, edge, early, put, pieces, back, together, discussed, difference, between, went, take, close, readable, utility, yet, guava, considerably, reduce, readability, pretty, though, vast, majority, pick, define, primitives, wow, lot, did, solve, corner, finally, comparing, depends, situation, personally, prefer, declaration, aware, alternatives, martin, odersky, lex, spoon, bill, venners, write, angelika, langer, klaus, kreft, aren, violating, trickier, analyze, name, isn, department, engineering, marketing, doesn, either, breaks, seems, occasionally, additional, overrides, inherits, includes, misses, violates, symmetry, requirement, pass, hence, continue, see, below, solves, mentioned, opens, worms, alternative, operator, extending, reasonable, extension, logging, functional, able, supertype, becomes, especially, relevant, spins, hibernate, spring, created, belong, requires, names, looking, tricky, detail, following, making, time, guards, nullpointerexception, further, down, strange, often, querying, matters, beginning, optimization, tight, loops, handle, persons, provide, course, create, specialized, general, strategy, choosing, overloaded, based, parameter, good, static, analysis, hierarchies, calls, instead, reason, why, expected, wat, happens, important, takes, common, variant, familiar, points, technicality, consistency, behave, erratically, complicate, multiple, invocations, provided, information, used, comparisons, modified, consistent, transitive, symmetric, reflexive, implements, little, formalization, saw, quote, yes, determine, conversely, anything, meaningful, exercise, futility, worked, basic, algebraic, relations, wait, don, need, duh, inspiring, mine, interesting, again, hardly, worth, mentioning, each, itself, help, think, encounter, daily, lives, laptops, hardware, specifications, adhere, specific, ill, kinds, happen, formal, moment, discuss, comes, into, play, while, aslist, arrays, notably, contain, element, something, suitable, character, sequence, dates, day, mean, determines, sameness, since, inherit, holds, hold, relationship, terms, ignore, bugs, every, literal, were, wrapped, interning, accordingly, piece, implemented, cover, read, after, separate, toc, org, table, contents, origin, lic, artist, dev, src, 2016, slides, upcoming, past, schedule, recordings, virtual, threads, vector, structured, concurrency, lilliput, galahad, babylon, openjdk, conversation, book, club, videos, jms, newsletter, blog, rant, jigsaw, jdeps, impulse, default, lang, review, comments, enabled, nipafx,


Text of the page (random words):
ls correctly nipafx this site works well without javascript but it works even better with javascript enabled bluesky mastodon discord youtube twitch github stackoverflow rss words tags architecture clean code clean comments code review collections community core lang core libs default methods deprecation documentation dop generics impulse j_ms java 10 java 11 java 12 java 13 java 16 java 17 java 18 java 20 java 23 java 24 java 25 java 26 java 27 java 8 java 9 java basics java next javafx jdeps js junit 5 junit pioneer lambda libfx libraries maven meta migration on ramp optional pattern matching patterns performance project amber project jigsaw project leyden project loom project panama project valhalla rant record args records reflection serialization streams switch techniques testing tools turn of the year var blog posts newsletter the jms videos tags ai architecture book club clean code collections community conversation core libs deprecation documentation dop generics j_ms java 10 java 11 java 12 java 16 java 17 java 18 java 19 java 21 java 22 java 23 java 24 java 25 java 26 java 27 java 28 java 8 java 9 java basics java next javafx junit 5 junit pioneer lambda libraries maven meta migration on ramp openjdk optional pattern matching patterns performance project amber project babylon project galahad project leyden project lilliput project loom project panama project valhalla records reflection serialization streams structured concurrency switch techniques tools turn of the year var vector virtual threads recordings streams schedule code demos demos demos junit pioneer record args libfx talks my talks past upcoming slides about about me license privacy 2016 06 06 how to implement equals correctly post java basics a fundamental aspect of any java class is its definition of equality it is determined by a class s equals method and there are a couple of things to be considered for a correct implementation let s check em out so we get it right src source dev artist lic license how to implement equals correctly origin i originally wrote this article for sitepoint s java channel check it out how to implement equals correctly table of contents identity versus equality thoughts on equality the equals contract implementing equals signature self check null check type check and cast field comparison summary final words share follow share this post with your community i m active on various platforms watch this space or follow me there to get notified when i publish new content org i originally wrote this article for sitepoint s java channel check it out how to implement equals correctly toc identity versus equality thoughts on equality the equals contract implementing equals signature self check null check type check and cast field comparison summary final words s f share this post with your community i m active on various platforms watch this space or follow me there to get notified when i publish new content a fundamental aspect of any java class is its definition of equality it is determined by a class s equals method and there are a couple of things to be considered for a correct implementation let s check em out so we get it right note that implementing equals always means that hashcode has to be implemented as well we ll cover that in a separate article so make sure to read it after this one identity versus equality have a look at this piece of code string some some string string other other string we have two strings and they are obviously different what about these two string some some string string other some boolean identical some other here we have only one string instance and some and other both reference it in java we say some and other are identical and accordingly identical is true what about this one string some some string string other some string boolean identical some other now some and other point to different instances and are no longer identical so identical is false we ll ignore string interning in this article if this bugs you assume every string literal were wrapped in a new string but they do have some relationship as they both have the same value in java terms they are equal which is checked with equals string some some string string other some string boolean equal some equals other here equals is true a variable s identity also called reference equality is defined by the reference it holds if two variables hold the same reference they are identical this is checked with a variable s equality is defined by the value it references if two variables reference the same value they are equal this is checked with equals but what does the same value mean it is in fact the implementation of equals that determines sameness the equals method is defined in object and since all classes inherit from it all have that method the implementation in object checks identity note that identical variables are equal as well but many classes override it with something more suitable for strings for example it compares the character sequence and for dates it makes sure that both point to the same day many data structures most notably java s own collection framework use equals to check whether they contain an element for example list string list arrays aslist a b c boolean contains list contains b the variable contains is true because while the instances of b are not identical they are equal this is also the point where hashcode comes into play thoughts on equality any implementation of equals must adhere to a specific contract or the class s equality is ill defined and all kinds of unexpected things happen we will look at the formal definition in a moment but let s first discuss some properties of equality it might help to think about it as we encounter it in our daily lives let s say we compare laptops and consider them equal if they have the same hardware specifications one property is so trivial that it is hardly worth mentioning each thing is equal to itself duh 2 there is another which is not much more inspiring if one thing is equal to another the other is also equal to the first clearly if my laptop is equal to yours yours is equal to mine 3 this one is more interesting if we have three things and the first and second are equal and the second and third are equal then the first and third are also equal again this is obvious in our laptop example that was an exercise in futility right not so we just worked through some basic algebraic properties of equivalence relations no wait don t leave that s already all we need because any relation that has the three properties above can be called an equality yes any way we can make up that compares things and has the three properties above could be how we determine whether those things are equal conversely if we leave anything out we no longer have a meaningful equality the equals contract the equals contract is little more but a formalization of what we saw above to quote the source the equals method implements an equivalence relation on non null object references it is reflexive for any non null reference value x x equals x should return true it is symmetric for any non null reference values x and y x equals y should return true if and only if y equals x returns true it is transitive for any non null reference values x y and z if x equals y returns true and y equals z returns true then x equals z should return true it is consistent for any non null reference values x and y multiple invocations of x equals y consistently return true or consistently return false provided no information used in equals comparisons on the objects is modified for any non null reference value x x equals null should return false by now the first three should be very familiar the other points are more of a technicality without consistency data structures behave erratically and being equal to null not only makes no sense but would complicate many implementations implementing equals for a class person with string fields firstname and lastname this would be a common variant to implement equals override public boolean equals object o self check if this o return true null check if o null return false type check and cast if getclass o getclass return false person person person o field comparison return objects equals firstname person firstname objects equals lastname person lastname let s go through it one by one signature it is very important that equals takes an object otherwise unexpected behavior occurs for example assume that we would implement equals person like so public boolean equals person person return objects equals firstname person firstname objects equals lastname person lastname what happens in a simple example person elliot new person elliot alderson person mrrobot new person elliot alderson boolean equal elliot equals mrrobot then equal is true what about now person elliot new person elliot alderson object mrrobot new person elliot alderson boolean equal elliot equals mrrobot now it s false wat maybe not quite what we expected the reason is that java called person equals object as inherited from object which checks identity why java s strategy for choosing which overloaded method to call is not based on the parameter s runtime type but on its declared type which is a good thing because otherwise static code analysis like call hierarchies would not work so if mrrobot is declared as an object java calls person equals object instead of our person equals person note that most code for example all collections handle our persons as objects and thus always call equals object so we better make sure we provide an implementation with that signature we can of course create a specialized equals implementation and call it from our more general one if we like that better self check equality is a fundamental property of any class and it might end up being called very often for example in tight loops querying a collection thus its performance matters and the self check at the beginning of our implementation is just that a performance optimization if this o return true it might look like it should implement reflexivity but the checks further down would be very strange if they would not also do that null check no instance should be equal to null so here we go making sure of that at the same time it guards the code from nullpointerexception s if o null return false it can actually be included in the following check like so if o null getclass o getclass return false type check and cast next thing we have to make sure that the instance we re looking at is actually a person this is another tricky detail if getclass o getclass return false person person person o our implementation uses getclass which returns the classes to which this and o belong it requires them to be identical this means that if we had a class employee extends person then person equals employee would never return true not even if both had the same names this might be unexpected that an extending class with new fields does not compare well may be reasonable but if that extension only adds behavior maybe logging or other non functional details it should be able to equal instances of its supertype this becomes especially relevant if a framework spins new subtypes at runtime e g hibernate or spring which could then never be equal to instances we created an alternative is the instanceof operator if o instanceof person return false instances of subtypes of person pass that check hence they continue to the field comparison see below and may turn out to be equal this solves the problems we mentioned above but opens a new can of worms say employee extends person and adds an additional field if it overrides the equals implementation it inherits from person and includes the extra field then person equals employee can be true because of instanceof but employee equals person can t because person misses that field this clearly violates the symmetry requirement there seems to be a way out of this employee equals could check whether it compares to an instance with that field and use it only then this is occasionally called slice comparison but this doesn t work either because it breaks transitivity person foo new person mr foo employee fu new employee mr foo marketing employee fuu new employee mr foo engineering obviously all three instances share the same name so foo equals fu and foo equals fuu are true by transitivity fu equals fuu should also be true but it isn t if the third field apparently the department is included in the comparison there is really no way to make slice comparison work without violating reflexivity or and this is trickier to analyze transitivity note apparently that s not quite true there are other way to make it work but they aren t exactly trivial check these posts for details implementing equals to allow slice comparison by angelika langer and klaus kreft how to write an equality method in java by martin odersky lex spoon and bill venners so we end with two alternatives use getclass and be aware that instances of the type and its subtypes can never equal use instanceof but make equals final because there is no way to override it correctly which one makes more sense really depends on the situation personally i prefer instanceof because its problems can not include new fields in inherited classes occurs at declaration site not at use site field comparison wow that was a lot of work and all we did was solve some corner cases so let s finally get to the test s core comparing fields this is pretty simple though in the vast majority of cases all there is to do is to pick the fields that should define a class s equality and then compare them use for primitives and equals for objects if any of the fields could be null the extra checks considerably reduce the code s readability return firstname person firstname firstname null firstname equals person firstname lastname person lastname lastname null lastname equals person lastname and this already uses the non obvious fact that null null is true it is much better to use java s utility method objects equals or if you re not yet on java 7 guava s objects equal return objects equals firstname person firstname objects equals lastname person lastname it does exactly the same checks but is much more readable summary we have discussed the difference between identity must be the same reference checked with and equality can be different references to the same value checked with equals and went on to take a close look at how to implement equals let s put those pieces back together make sure to override equals object so our method is always called include a self and null check for an early return in simple edge cases use getclass to allow subtypes their own implementation but no comparison across subtypes or use instanceof and make equals final and subtypes can equal compare the desired fields using objects equals or let your ide generate it all for you and edit where needed final words we have seen how to ...
Images from subpage: "nipafx.dev/collections/" Verify
Images from subpage: "nipafx.dev/community/" Verify
Images from subpage: "nipafx.dev/core-lang/" Verify
Images from subpage: "nipafx.dev/core-libs/" Verify
Images from subpage: "nipafx.dev/default-methods/" Verify

Verified site has: 96 subpage(s). Do you want to verify them? Verify pages:

1-5 6-10 11-15 16-20 21-25 26-30 31-35 36-40 41-45 46-50
51-55 56-60 61-65 66-70 71-75 76-80 81-85 86-90 91-95 96-96


The site also has references to the 1 subdomain(s)

  slides.nipafx.dev  Verify


Top 50 hastags from of all verified websites.

Supplementary Information (add-on for SEO geeks)*- See more on header.verify-www.com

Header

HTTP/2 301
server GitHub.com
content-type text/html
location htt????/nipafx.dev/implement-java-equals-correctly/
access-control-allow-origin *
expires Mon, 14 Sep 2026 21:27:37 GMT
cache-control max-age=600
x-proxy-cache MISS
x-github-request-id D6D8:ABA20:162A1E0:1647F38:6AA86471
x-github-edge-region fra
accept-ranges bytes
age 0
date Mon, 14 Sep 2026 21:17:37 GMT
via 1.1 varnish
x-served-by cache-rtm-ehrd2290024-RTM
x-cache MISS
x-cache-hits 0
x-timer S1789420658.578702,VS0,VE99
vary Accept-Encoding
x-fastly-request-id c29708484f77bbde4b614130f53b010fe86f9399
content-length 162
HTTP/2 200
server GitHub.com
content-type text/html; charset=utf-8
last-modified Mon, 14 Sep 2026 15:39:57 GMT
access-control-allow-origin *
etag W/ 6aa8154d-340ce
expires Mon, 14 Sep 2026 21:27:37 GMT
cache-control max-age=600
content-encoding gzip
x-proxy-cache MISS
x-github-request-id 4EBC:20A79D:15F17EF:160F572:6AA86471
x-github-edge-region fra
accept-ranges bytes
age 0
date Mon, 14 Sep 2026 21:17:37 GMT
via 1.1 varnish
x-served-by cache-rtm-ehrd2290024-RTM
x-cache MISS
x-cache-hits 0
x-timer S1789420658.686509,VS0,VE124
vary Accept-Encoding
x-fastly-request-id 9fa398de79f9bca7326231ad623660cc10a4fac9
content-length 49630

Meta Tags

title="How To Implement equals Correctly // nipafx"
charset="utf-8"
http-equiv="x-ua-compatible" content="ie=edge"
name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"
name="generator" content="Gatsby 5.7.0"
data-react-helmet="true" name="description" content="A fundamental aspect of any Java class is its definition of equality. It is determined by a class's equals method. Let's see how to implement it correctly."
data-react-helmet="true" name="twitter:card" content="summary_large_image"
data-react-helmet="true" name="twitter:site" content="@nipafx"
data-react-helmet="true" name="twitter:creator" content="@nipafx"
data-react-helmet="true" name="twitter:title" content="How To Implement equals Correctly // nipafx"
data-react-helmet="true" name="twitter:description" content="A fundamental aspect of any Java class is its definition of equality. It is determined by a class's equals method. Let's see how to implement it correctly."
data-react-helmet="true" name="twitter:image" content="htt????/nipafx.dev/static/9ef0ca98e7fee9872f7a0de5ac512eff/880e6/implementing-equals-correctly.jpg"
data-react-helmet="true" name="keywords" content="equals"
data-react-helmet="true" property="og:title" content="How To Implement equals Correctly // nipafx"
data-react-helmet="true" property="og:type" content="article"
data-react-helmet="true" property="og:image" content="htt????/nipafx.dev/static/9ef0ca98e7fee9872f7a0de5ac512eff/880e6/implementing-equals-correctly.jpg"
data-react-helmet="true" property="og:url" content="htt????/nipafx.dev/implement-java-equals-correctly/"
data-react-helmet="true" property="og:description" content="A fundamental aspect of any Java class is its definition of equality. It is determined by a class's equals method. Let's see how to implement it correctly."
data-react-helmet="true" property="og:site_name" content="nipafx // You. Me. Java."
name="theme-color" content="#69ea7d"

Load Info

page size213198
load time (s)0.384311
redirect count1
speed download129244
server IP 185.199.108.153
* all occurrences of the string "http://" have been changed to "htt???/"