News for hackers. Stuff that matters.
Re:What if null was an Object in Java? by alkonaut on Sunday April 28, 2024 @12:42PM (#40189825)
Wait, javas Optional is a reference type so it can be null? Doesn’t that almost defeat the purpose of it?
Re:What if null was an Object in Java? by michaelt on Sunday April 28, 2024 @01:03PM (#40189992)
Arguably yes, but that doesn't stop people using it.

Basically Java had nulls from the start. A decade or so later some people who didn't like nulls introduced their own Optional type, as a third-party library. Enough people liked it that Optional was added to Java's standard library.

But as it's just an object, it can be null. Some null avoidance enthusiasts also use third-party @Nullable and @NotNull annotations, which some automated code checking tools will attempt to verify during compile/test.

Re:What if null was an Object in Java? by pgwhalen on Sunday April 28, 2024 @01:24PM (#40190165)
It doesn’t defeat the problem in theory, but in my experience it does in practice. I’ve never come across an NPE on a nullable reference even in development - it would have to be the result of a really fundamental misunderstanding of the concept.

YMMV. Obviously it depends on your teammates.

Re:What if null was an Object in Java? by Defletter on Sunday April 28, 2024 @12:50PM (#40189892)
Yup. Your IDE will likely highlight it as an issue, but it's totally legal to return a null Optional. There's nothing special about it, it's just a wrapper class.
Re:What if null was an Object in Java? by alkonaut on Sunday April 28, 2024 @12:55PM (#40189934)
Did the project to add value types to Java (I’m sure I heard of it a decade ago) never finish?
Re:What if null was an Object in Java? by Defletter on Sunday April 28, 2024 @01:00PM (#40189974)
Not yet, that's Project Valhalla iirc. It's coming along but hasn't been merged yet. I don't believe it's even a preview feature within the JDK yet.
Re:What if null was an Object in Java? by steve_rambo on Sunday April 28, 2024 @05:23PM (#40192099)
Re:What if null was an Object in Java? by paulddraper on Sunday April 28, 2024 @01:04PM (#40189999)
Kinda.

Every non-primitive is nullable in Java. Adding Optional doesn't/can't change that.

You can have a gentlemen's agreement to prefer None to null.

Re:What if null was an Object in Java? by mrkeen on Sunday April 28, 2024 @05:57PM (#40192363)
It gives you the ability to treat nulls as something-to-fix.

In a team without Optionals, every time you touch a null that you didn't expect, you have to decide "Is this deliberately null, or was it a mistake?" Without that knowledge, you don't know whether your code should assert against the null, or allow it to pass through as a valid value.

With Optionals, it becomes much simpler to cut through that nonsense. A null is a bug and you fix it (with the exception of json at the boundaries of your system, etc.) If you do find a value where you change your mind about its nullability, changing it to/from Optional will give you compile errors in exactly those other parts of the code that you now have to check/change.