3 ms·
Dereferencing null is undefined behaviour in C. Probably it could be handled with platform-specific code, but handling it correctly not the easiest thing to do.
by vbezhenar 4y ago
Dereferencing null is undefined behaviour in C. Probably it could be handled with platform-specific code, but handling it correctly not the easiest thing to do. Other than dereferencing null, there're so many ways to accidentally blow up C code and something like reading uninitialised memory is truly undefined behaviour which can't be worked around.
Java does not have undefined behaviour at all. Dereferencing null would throw NPE which is ordinary exception, completely fine to handle or suppress or whatever. There's no concept of uninitialised memory. The only sources of undefined behaviour in Java is calling native code or using unsafe methods which are very rare and usually located in well tested library code.
Even stack overflowing is defined behaviour and you can easily recover from it.
- tomohawk 4y agoI've never seen Java code in production handle NPEs, OOMs or other such conditions well. It's a shame Java does not have value types, which makes nulls a much rarer thing to have to deal with.
- icedchai 4y agoIn theory, yes, but when you run low on memory (heap, PermGen space, whatever), you can run into unusual situations. You may have a bunch of threads spinning with exceptions, continually retrying, making no progress. You'll have no choice but to kill -9 your process and restart.