Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 565941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:55:31+00:00 2026-05-13T12:55:31+00:00

I have always understood there to be two types of equality in Java, value

  • 0

I have always understood there to be two types of equality in Java,

  • value equality : uses the .equals() method to test that two objects implement an equivalence relation on non-null object references.
  • reference equality : uses the == operator to test that two primitive types or memory locations are equal.

The following pages describe these language fundamentals in more detail.

  • Wikibooks Java Programming : Java Programming/Comparing Objects
  • xyzws Java EE FAQ : What are the differences between the equality operator and the equals method?
  • Java Platform API : Javadoc for Object.equals()
  • Java Language Specification : Equality Operators

What none of these links explicitly specify is what should happen if two null object references are compared for value equality. The implicit assumption is that a NullPointerException should be thrown but this is not what is done by the ObjectUtils.equals() method, which might be considered a best practice utility method.

What worries me is that Apache Commons seems to have effectively introduced a third measure of equality into Java by the back door and that the already confusing state of affairs might have been made greatly more complex. I call it a third measure of equality because it attempts to test for value equality and when that fails it falls back to testing for reference equality. The Apache Commons equality test has many similarities with the value equality and reference equality but is also distinctly different.

Am I right to be concerned and to want to avoid using the ObjectUtils.equals() where ever possible?

Is there an argument for claiming that ObjectUtils.equals() provides a useful union of the other two measures of equality?

Chosen Answer

There doesn’t seem to be a consensus opinion on this question but I decided to mark Bozho’s as correct because he best drew my attention to what I now see as the greatest problem with null-safe equals checks. We should all be writing fail-fast code that addresses the root cause of why two null objects are being compared for value equality rather than trying to sweep the problem under the carpet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:55:31+00:00Added an answer on May 13, 2026 at 12:55 pm

    Here’s the code of ObjectUtils.equals(..):

    public static boolean equals(Object object1, Object object2) {
         if (object1 == object2) {
           return true;
         }
         if ((object1 == null) || (object2 == null)) {
           return false;
        }
        return object1.equals(object2);
    }
    

    ObjecUtils docs state clearly that objects passed can be null.

    Now on the matter whether true should be returned if you compare two nulls. In my opinion – no, because:

    • when you compare two objects, you are probably going to do something with them later on. This will lead to a NullPointerException
    • passing two nulls to compare means that they got from somewhere instead of “real” objects, perhaps due to some problem. In that case comparing them alone is wrong – the program flow should have halted before that.
    • In a custom library we’re using here we have a method called equalOrBothNull() – which differs from the equals method in this utility in the null comparison.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 342k
  • Answers 342k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't believe things that are radically unrelated should even… May 14, 2026 at 5:10 am
  • Editorial Team
    Editorial Team added an answer Try the cex argument: ?par cex A numerical value giving… May 14, 2026 at 5:10 am
  • Editorial Team
    Editorial Team added an answer You need to load Template Toolkit first, with: use Template;… May 14, 2026 at 5:10 am

Related Questions

I'm kind of curious how I should approach a problem with iPhones and communication
I find myself very intrigued by the existence of a ConcurrentBag<T> class in the
Okay, I get it. Data in PostgreSQL is case-sensitive. And I know I can
Update: This is, as I was told, no principle Python related problem, but seems
I'm coding a game, and I'd like to be able to find the center

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.