Live data from Hacker News

What if null was an Object in Java?

donraab.medium.com

161–164 of 164 posts

Re: What if null was an Object in Java?

#161

Earlier quoted context omitted.

null would just mean the zero value instead of the absence of a value String foo = null; String bar = ""; foo.equals(bar) --> true This works well provided the data type has a sensible zero value like collection types EDIT: I'm blocked from posting so I won't be responding further, thank you for discussion.

In the end, you'll have a mixture of NULL and "" in your DB, and a couple of years later a piece of logic written in another language will fail spectacularly.

> In the end, you'll have a mixture of NULL and "" in your DB

Not if you use Oracle.

Re: What if null was an Object in Java?

#162
post #160

The problem isn't really that null isn't an object, it's that the type system allows any object reference of a particular class to also be null. After using Typescript for the past couple years, it's such a joy when you define a variable of type Foo and know it won't contain null. Granted, TS also has to deal with undefined vs null weirdness (and even more weirdness in that an object not containing a property is subt…

> it's such a joy when you define a variable of type Foo and know it won't contain null Why is that? I've never seen a good explanation of this point of view. This all feels like just syntactic sugar to me. Sure you might not have null, you might have an empty object or any other variation. At the end of the day, if a value was expected to be available but is not available (for whatever reason), reliable code is goin…

The point is that every function is explicitly telling you if it can or can not return null. Your editor and typechecker will tell you if you need to handle null after calling that function.

If you have a non-null returning function that you need to change so it can return null, then your typechecker will tell you all the places where you now need to handle the new null return.

It doesn't need to be a very large codebase before this becomes a very useful tool to help when refactoring.

Re: What if null was an Object in Java?

#163

I quite like the way dart handles nulls.

The way Crystal and Dart 2 handles it? Null as a separate and unique type unrelated to Object where type unions solve the problem using syntax like `String?` that is equal to `String | Null`.

Yeah, means I don't need to worry about null unless I explicitly allow null.

Re: What if null was an Object in Java?

#164
post #61
post #51

Earlier quoted context omitted.

Null is the absence of a value. How do to distinguish 0 from no value?

The point is to eliminate the idea of an absence of a value. A variable is always assigned to a value, but there is a special value called null which behaves as a kind of sentinel value whose methods all return the null value for their respective type.

> The point is to eliminate the idea of an absence of a value

The whole point is to track the absence of a value. Why would one want to eliminate it?

null is way more trackable that some other special value like 0, -1, 999, "", etc.

Post reply on HN