0

Related to the following conversations -

The second link suggests a few ways to avoid != null statements.

  1. Using assert is one.
  2. Using NullObjectPattern - Not an option, since everyone dealing isn't in charge of production code.

The first link just suggests != null idea.

Object class has requiresNonNull methods which throw NullPointerException which was gonna be thrown anyway if the object was used. assert also proposes same way. If I could handle every exception then, the code would get ugly with way too many try and catches.

Using != null isn't very pretty in an object oriented sense.

I think Object.isNull and Object.isNotNull are ways to go to make code look concise and neat with also handling the situation well (I mean without verbose try...catch statements). I can easily use them in any conditional statements. This is much better than raw != null.

But, why are there no such methods? Is passing around null such a bad idea? If it is then what should we do if null is a valid response instead of != null?

Edit:

Changed the question from: Why is there no boolean Object.isNull(Object object) static utility method in Java? to Is there a boolean Object.isNull(Object object) or something similar static utility method in Java?

Community
  • 1
  • 1
Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64
  • 2
    I don't get what is the problem with using `obj == null` and `obj != null`. `Objects.isNull` and `Objects.nonNull` both use `return obj == null` and `return obj != null` internally. – BackSlash Apr 11 '17 at 09:22
  • There's nothing inherently wrong with the code `if (x != null)` in many contexts. You should read that second link a bit more in-depth, as (to my eyes) the point being made is more that you shouldn't be returning `null` willy-nilly from your own code in a way which fill force client code to be overly defensive. Adding the verbosity of `Objects.isNull` really only has value when working with streams or some other context where method references are used. Blindly applying a statement like "== null is bad code" in every situation is just silly. – CollinD Apr 11 '17 at 09:22
  • (a) You seem to be confusing `Object` and `Objects`. (b) In Java 8, there are `Objects.isNull` and `Objects.nonNull`. – RealSkeptic Apr 11 '17 at 09:22
  • @RealSkeptic I searched for `Object.isNull` and got `Object.nonNull`. I didn't find `Objects.isNull` – Tarun Maganti Apr 11 '17 at 09:23
  • See [Objects.isNull](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#isNull-java.lang.Object-) – Fildor Apr 11 '17 at 09:25
  • Well, how about [here](http://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#isNull-java.lang.Object-)? And you are still confusing `Object` and `Objects`. There is no `Object.nonNull`. – RealSkeptic Apr 11 '17 at 09:25
  • "what should we do if null is a valid response instead of != null?" - See http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html Also, you already came across NullObjectPattern. So this should answer at least that part of your question? – Fildor Apr 11 '17 at 09:27
  • 4
    Also, you should never use a `try-catch` for checking nulls. `obj != null` is the correct way to check if an object is `null`, using a `try-catch` for null checking is just bad design. – BackSlash Apr 11 '17 at 09:28
  • @CollinD The second link said, If there is no option, I have to check for null using whatever means available to me(which at the time of asking `Objects.isNull` didn't exist). I [couldn't find `Objects.isNull`](https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=object+is+null+java) today in Google. – Tarun Maganti Apr 11 '17 at 09:32
  • @Fildor I am not in charge of production code. `NullObjectPattern` is not useful when you are dealing with other's code. – Tarun Maganti Apr 11 '17 at 09:36
  • I don't get why I got a downvote. I tried in [DuckDuckGo](https://duckduckgo.com/?q=Object+is+null+java&t=ha&ia=qa), and [Google](https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=object+is+null+java) and couldn't find `Objects.isNull()` in them. It was a genuine question. I should atleast know why it's downvoted. – Tarun Maganti Apr 11 '17 at 09:39
  • @BackSlash I agree with you. This is why for me, `Objects` is reserved to Stream. Here is why [`Objects.requireNotNull`](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#requireNonNull-T-), it throw a NPE if it is null, so unless you catch it, you are f... (And it exist from 1.7...) – AxelH Apr 11 '17 at 09:41
  • @Fildor [Another reason why NullObjectPattern isn't preferred](https://en.wikipedia.org/wiki/Null_Object_pattern#Criticism) – Tarun Maganti Apr 11 '17 at 09:41
  • I've downvoted your question because it shows a lack of research as well as being primarily opinion based ("Why is there no X?" (which only the committee which designed the Java internals can answer) rather than "Is there a functionality similar to X" which anyone can answer) I've also voted to close it for the same reason. And obviously this question is generating a ton of chatter in the comments which is also generally not productive. – CollinD Apr 11 '17 at 09:42
  • 1
    @TarunMaganti To me, it is trivial to assume that you *know what you are doing* when using it - and when you shouldn't. I won't tell a carpenter not to use a Hammer because he could hurt his thumb ... – Fildor Apr 11 '17 at 09:45
  • @CollinD Ok. It is fine to edit the question, to make it as `something similar to` type? – Tarun Maganti Apr 11 '17 at 09:47
  • "I am not in charge of production code." - you should have stated that in the question. It is not clear *why* it is not an option for you. – Fildor Apr 11 '17 at 09:48

1 Answers1

3

https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html:

static boolean isNull(Object obj) Returns true if the provided reference is null otherwise returns false.

static boolean nonNull(Object obj) Returns true if the provided reference is non-null otherwise returns false.

Since 1.8, though...

Mikhail Antonov
  • 1,297
  • 3
  • 21
  • 29
  • I didn't find `Objects`. I tried `Object.isNull` and `Object.isNotNull` in Google – Tarun Maganti Apr 11 '17 at 09:20
  • Both methods internally do `return obj == null` and `return obj != null`. I still don't get why one would avoid using `var == null` in code. – BackSlash Apr 11 '17 at 09:21
  • 2
    @BackSlash they can be used as method references with streams. Code looks much cleaner... `someList.stream().filter(Objects::nonNull).forEach(...)` – Mikhail Antonov Apr 11 '17 at 09:23
  • 2
    @MikhailAntonov That's exactly why it only appear in Java 8. It was useless before the method references system – AxelH Apr 11 '17 at 09:25
  • 3
    @MikhailAntonov In this specific case it makes sense! But OP didn't specify this in the question, he spoke generally about "checking nulls in object-oriented language", and as the OP says "Using != null isn't very pretty in an object oriented sense." - which seems a totally nonsensical point to me. – BackSlash Apr 11 '17 at 09:27
  • @BackSlash Because a lot of null checks may get you ugly code. Ref - [Oracle reason for optional](http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html). Also - [C.A.R Hoare's own words](https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions) – Tarun Maganti Apr 11 '17 at 10:10
  • 1
    @TarunMaganti Using Null checks or not can have many reasons. Some of them good, other not so good. It is one of those "there is no black or white" things. In some usecases it may be reasonable to just have a null and check it and others where you certainly want to avoid that. There is no "it is *always* bad" or "*always* good". Speaking of the null-Reference as such is a different story. In some way you'd always have to have a concept of "not initialized" - and to check for it. Hoare is just regretting having chosen to do it the way they did. – Fildor Apr 11 '17 at 10:13
  • @Fildor But, sometimes there are good and bad practices, right? This situation seems like it is not that. How would I know unless something like this happened? – Tarun Maganti Apr 11 '17 at 10:18
  • 1
    @TarunMaganti I agree, but your other options don't improve code readability. You can use `assert obj != null; assert obj.property != null; obj.property.method();`, or `if(obj != null && obj.property != null) obj.property.method()` or `if(Objects.nonNull(obj) && Objects.nonNull(obj.property)) obj.property.method()` - They all will be harder to read, and if you have to choose the less-hard to read you'll choose the `!= null` option. And yes, I didn't mention try-catch because it's not an option, using for null-checking is bad programming. – BackSlash Apr 11 '17 at 10:23
  • 2
    Also, asserts may be ignored by jvm if run without `-ea` flag – Mikhail Antonov Apr 11 '17 at 10:25
  • 1
    @TarunMaganti In your specific case there is another vector to it: You do not have control over the overall design. So your options are narrowed down. Sometimes that can be the cause for you to being forced to use bad practice even if it hurts your good-developer-feelings. Of course you can try and make the best effort to use good practice but you also have to keep in mind at which cost that will come. As BackSlash sais: There is no real benefit in going from `if( obj != null)` to `if ( Objects.nonNull(obj))`. But if you do, you take some minutes (or hours?) to change all the existing code ... – Fildor Apr 11 '17 at 10:36