I am wondering why the Exception in the following bytecode (used to throw an Exception) is duplicated.
NEW java/lang/IllegalArgumentException
DUP
INVOKESPECIAL java/lang/IllegalArgumentException <init> ()V
ATHROW
I'll analyze this line by line where [] = new stack after that op is used:
IllegalArgumentException onto the stack [SomeIllegalArgumentException]In byte code, an object is first created by class, and then a constructor is called on that object. The signature of a constructor ends with V for void as it does return anything. This means a copy of the original reference to the object must be kept on the stack (or in a variable) so it can be thrown after the constructor is called.
BTW The internal name for a constructor is <init> and the internal name for a static initialiser code is <clinit>
NEWjust allocates object memeory without calling any constructor,INVOKESPECIALwill call a construction but that call will pop from a stackthispointer and push nothing (constructors returnvoid). By convention compilers targeting JVM will generate aboveNEW/DUP/INVOKESPECIALsequence to create a new object. This is a pattern that is recognized by JVM and may be specially optimized...