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

  • SEARCH
  • Home
  • 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 7747031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:25:46+00:00 2026-06-01T10:25:46+00:00

Possible Duplicate: A better way to compare Strings which could be null I have

  • 0

Possible Duplicate:
A better way to compare Strings which could be null

I have an if condition which looks like this :

if( !str1.equals(str2) )
{
    ---
    ---
    ---
}

where str1 and str2 are two string objects.

There are chances that str1 might be null , so is the below code equivalent to the above, along with handling the null check?

if( !(str1==null ? str2==null : str1.equals(str2)) )
{
    ---
    ---
    ---
}

Thanks!

  • 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-06-01T10:25:47+00:00Added an answer on June 1, 2026 at 10:25 am

    Yes, that will lead to the same result.

    To be a bit more specific:

    1. If str1 isn’t null, it’s exactly the same, since it just passes through the ternary check to the same expression as before
    2. If str1 is null, it then becomes a check to see if str2 is also null.

    And since you have the whole ternary expression wrapped up with the ! out front, that behaves the same as before.

    If you wanted to be a bit more clear, you could make str2==null into an actual comparison between str1 and str2: str1==str2. Since one of the values is already null, it doesn’t matter that it’s a referential check instead of a proper string equality check, and ends up being a bit more clear in the code (to me, anyways)

    As others have mentioned, however, the Apache Commons library already includes this null-safe equality capability, but it does require a rather substantial library inclusion. On the other hand, many feel that the Apache Commons functionality should be effectively considered a part of Java itself, so you can decide for yourself if you want the extra dependency.

    Lastly, the functionality isn’t technically equivalent, since the default .equals() method will throw a NullPointerException, while your equality check code won’t. If that is the behavior you were looking for (which I assume it is), then you’re fine, but it is something to be aware of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Better way of converting a Map[K, Option[V]] to a Map[K,V] I have
Possible Duplicate: C# - Is there a better alternative than this to ‘switch on
Possible Duplicate: C++ STL: Which method of iteration over a STL container is better?
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: How to validate domain name in PHP? Better way to validate a
Possible Duplicate: Saving images: files or blobs? im building a site which will have
Possible Duplicate: Is there an “exists” function for jQuery Is there a better way
Possible Duplicate: Is there a better way than parsing /proc/self/maps to figure out memory
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions

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.