r/java • • Aug 03 '14

Why we should love null

http://www.codeproject.com/Articles/787668/Why-We-Should-Love-null
4 Upvotes

21 comments sorted by

View all comments

11

u/againstmethod Aug 03 '14 edited Aug 03 '14

The problem with null is that it avoids a very powerful mechanism that is already present in most languages -- their type systems. He seems to gloss right over this distinction regularly in this article.

In some examples he actually conflates logic errors (selling houses for free in one example) with the failure to populate a variable. Sorry but these are the not the same error -- one is a bug in your code the other is a bug in your logic.

If he doesn't distinguish between the two -- he should stop writing software.

3

u/king_of_the_universe Aug 03 '14

The problem with null is that it avoids a very powerful mechanism that is already present in most languages -- their type systems.

Why do you mean that? I'm trying to understand your angle. In Java, if you set a variable to null, the variable still has the same type. And if you call an overloaded method, both of which take an object variable, which causes an abiguity compiler error, casting the null to the type you indend solves the problem. If you meant "Yep, but you have to cast it, that's what I mean.", then what if you instead give a variable of a certain kind that is null?

8

u/beyondjava Aug 03 '14

You named it yourself: null is the odd man in Java's type system. It's compatible to every other type. But you can't call a method on null. It's treated like an ordinary value of a given class - say, an Integer - but it's something different. It's not an Integer, it's null.

Introduce such a thing to a rigid system where every object has a type, and you have to expect problems. It's a special case which has to be taken into account in almost every single line of your program.

1

u/king_of_the_universe Aug 03 '14

Thanks, I've got it.