Why does Java have an IINC
bytecode instruction?
There is already an IADD
bytecode instruction that can be used to accomplish the same.
So why does IINC
exist?
Only the original designers of Java can answer why they made particular design decisions. However, we can speculate:
IINC
does not let you do anything that can't already be accomplished by a ILOAD
/SIPUSH
/IADD
/ISTORE
combo. The difference is that IINC
is a single instruction, which only takes 3 or 6 bytes, while the 4 instruction sequence is obviously longer. So IINC
slightly reduces the size of bytecode that uses it.
Apart from that, early versions of Java used an interpreter, where every instruction has overhead during execution. In this case, using a single IINC
instruction could be faster than the equivalent alternative bytecode sequence. Note that JITting has made this largely irrelevant, but IINC
dates back to the original version of Java.