0

One of my favorite things about Kotlin, relative to Java, is that you can use the compiler to guarantee that certain values are never null. (Swift also has this feature.)

 var foo: Thing 
 var bar: Thing?

Is that example, foo can never be null. I don't have to pollute my code with of defensive null checks. On the other hand, bar could be null. There is also convenient syntax for checking if something is null and using it.

bar?.x             // bar.x, or null if bar is null
bar ?: defaultBar  // bar, or defaultBar if bar is null
if (bar != null) { 
    // bar is now Thing here, not Thing? (Assuming bar is a parameter or local variable.)
}

Java has been moving faster lately, and adding features, but I haven't heard anything about this. Was it considered and rejected? Or might it someday be added to Java?

Rob N
  • 15,024
  • 17
  • 92
  • 165
  • 1
    No kotlin expert here. But I think you can do the same with Optionals in java. `Optional.of()` and `Optional.ofNullable()` . – Eritrean May 04 '20 at 17:42
  • Optional is no where near as good, because its use is not proven to be correct by the type checker. (And it's also more verbose.) – Rob N May 04 '20 at 17:44

3 Answers3

2

No, Oracle has no such plans.

Oracle feels that you can use a third-party tool to do so, such as the Checker Framework, NullAway, or support built into an IDE such as IntelliJ or Eclipse.

Oracle doesn't even support the creation of a standard @NonNull annotation that all third-party tools would use. Oracle let JSR 305, which would have defined standard annotations, expire and has not revived it.

mernst
  • 7,437
  • 30
  • 45
  • That's too bad. I don't know anyone who has used and understood this feature that doesn't think it's a big improvement. But, I admit I haven't used those add-on frameworks. Maybe they can get a lot of the benefit. I will check them out. – Rob N May 05 '20 at 17:07
  • @RobN Oracle's attitude is that good third-party tools exist and that is sufficient. Also, Oracle wants the third-party tools to fight it out in the marketplace rather than Oracle having to make its own assessment of which is best-suited for Java programmers. (Source: discussions with the Chief Architect of Oracle's Java Platform Group.) – mernst May 05 '20 at 21:10
0

There are 3 static methods in Objects class for requiring object to be not null and throwing exception if it is:

requireNonNull(T obj)

requireNonNull(T obj, String message)

requireNonNull(T obj, Supplier<String> messageSupplier)
nVinz
  • 1
  • 1
  • 1
    That checks for null at _runtime_, but what I'm talking about is compile time. Just like when you declare a variable is `String` in Java, you don't have to check at runtime to see if maybe it's an `Integer`. Likewise, in languages with this type checked null/not-null feature (like Kotlin, Swift, and others), if you declare it as `String` instead of `String?`, then you _know_ it is never null, and you don't have to check it at runtime. – Rob N May 05 '20 at 17:01
0

Like @mernst pointed out, you can use the Checker Framework. It checks everything at compile time and you can even add your own checker.

Microtribute
  • 962
  • 10
  • 24