Unboxing Null-Object to primitive type results in NullPointerException, fine?

后端 未结 4 1667
Happy的楠姐
Happy的楠姐 2020-12-03 09:29

This snippet throws an NullPointerException due to the fact that its unboxed to a primitive type and Long.longValue() is called, right?

Tha

4条回答
  •  一整个雨季
    2020-12-03 10:10

    That's not what IllegalArgumentException means. The compiler has no guarantee that the value will be null until runtime. All it knows is the type, which in your example is likely to be String.

    Certainly at runtime, when the exception is thrown, the compiler knows that the problem is a null value. You can see this yourself if you're using a debugger. So from a technology standpoint -- and here is the short answer to your question -- yes, it would be possible to create a compiler that will include that in the error description. But if you want a special message for null values, what's next? Special messages for integers that are outside some acceptable bound by more than 10? Admittedly that's kind of a silly example, but I hope it's illustrative.

提交回复
热议问题