Is it possible to produce undefined behavior when dereferencing `null` in Java?

前端 未结 4 1840
一生所求
一生所求 2021-02-20 03:28

I just learned that dereferencing null in C and C++ can sometimes produce undefined results. This is very intriguing to me, like all bizarre programming behaviors

4条回答
  •  星月不相逢
    2021-02-20 04:05

    The behavior is defined in 15.12.4.4 Locate Method to Invoke:

    Otherwise, an instance method is to be invoked and there is a target reference. If the target reference is null, a NullPointerException is thrown at this point. Otherwise, the target reference is said to refer to a target object and will be used as the value of the keyword this in the invoked method. The other four possibilities for the invocation mode are then considered.

    Dereferencing null should throw a NullPointerException.

提交回复
热议问题