From the JVM specs (Chapter 4.1 "The ClassFile structure"), it is stated that "The constant_pool table is indexed from 1 to constant_pool_count - 1."
I'm curious why they're skipping [0] and what is this entry reserved for.
They skipped index 0 so that it can be used for cases where you would normally reference a constant pool entry, but instead want to indicate "nothing". It is the constant pool equivalent of a null pointer.
The most notable use for index 0 is for "catch all" exception handlers. An exception handler can either point to the constant pool entry for the class of exceptions it wants to handle, or just use index 0 to catch everything (this is equivalent to catching java/lang/Throwable
). In practice, the compiler will generate catch all exception handlers to implement finally
, synchronized
blocks, and the cleanup portions of try with resources, among other things.
Other uses for index 0 include:
java/lang/Object