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

前端 未结 4 1836
一生所求
一生所求 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条回答
  •  旧时难觅i
    2021-02-20 04:25

    The JLS is not specific on how the null reference is implemented but it specifies its behavior. In other words, no there is no unspecified behavior. If you encounter a behavior other than specified in the JLS, it’s a bug.

    Let me clarify this: you can use native code to trash certain structures to let the JVM crash but that has nothing to do with any Java behavior anymore. But on typical JVM implementation, the implementation of the null behavior is the last thing you can disturb. Not, that it matters, what you trash if you override arbitrary memory from native code.

    “Unspecified behavior” means that the specification itself lets there room for differences in the resulting behavior. This is not the case with Java.

提交回复
热议问题