Reputation: 5098
How does the Java Virtual Machine know when to throw a NullPointerException? Does it check before every method I call on a an object if the object is null to check if it has to throw a NullPointerException? And if so, isnt this realy slow?
Upvotes: 3
Views: 1521
Reputation: 719376
There are two approaches that the JVM could use to "detect" the nulls.
The JVM could test explicitly the value of the reference before using it. This represents an overhead, but the JIT compiler could optimize away some of the null tests. For example:
this
it cannot give an NPE. If p
is not volatile and you write this:
if (p != null) { p.something(); }
then, the implied null check in p.something()
is not needed.
If p
is not volatile and you write this:
p.something();
p.somethingElse();
and then the second implied null check is not needed.
It is also worth noting that a null test could have zero performance impact on a CPU with a deep pipeline. It will depend on the localized memory access patterns, and whether the compiler / hardware is able to schedule the test instruction to overlap with (say) a memory fetch or store.
The JVM could implement the null check using virtual memory hardware. The JVM arranges that page zero in its virtual address space is mapped to a page that is unreadable + unwriteable. Since null
is represented as zero, when Java code tries to dereference null
this will try to access a non-addressible page and will lead to the OS delivering a "segfault" signal to the JVM. The JVM's segfault signal handler could trap this, figure out where the code was executing, and create and throw an NPE on the stack of the appropriate thread.
This is complicated and rather expensive. However, the expense does not matter provided that NPEs occur sufficiently rarely in the application.
Upvotes: 10
Reputation: 41
NULLPointerException is basically a run time exception i.e. JVM is expecting an actual object to be referenced when you are performing any operation.
To understand this better you need to know how object being created and referenced in java.Here is the syntax: myClass o = new myClass();
In this case, an object is being created in the heap space with o as reference to that object i.e o is basically pointing to that object. During run time, JVM determines the actual object being pointed by o and used that to perform the operation.
So lets just say you have a method doSomething() as an instance method for the above myClass defintion, and you are calling it like this o.doSomething(), so JVM will find the actual object and called this method.
But if you set o = null; then o is not pointing to actual object of myclass i.e. o is pointing to nothing. So now during execution of o.doSomething, JVM find out that o is pointing to nothing and throws the runtime exception NULLPointerException in that case.
Its not slow, because its the way JVM is being designed, to operate on the actual referenced objects which happens at runtime and if you are not handling the NULL check proeprly in your program and NULLpointerException being thrown by JVM then your program crashes and jvm terminates.
see java doc for more info: http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html
Upvotes: 0