Fundamentals 5 min read

Why i++ Prints 8: A JVM Bytecode Walkthrough of Java Increment Operators

This article explains how the i++ and ++i operators are implemented at the JVM bytecode level, using concrete code examples and step‑by‑step analysis of each instruction to show why i++ can output 8 while ++i yields 9.

Ubiquitous Tech
Ubiquitous Tech
Ubiquitous Tech
Why i++ Prints 8: A JVM Bytecode Walkthrough of Java Increment Operators

Java’s increment operators i++ and ++i appear similar but differ in their JVM execution. The article starts with a simple program (Test1) where int i = 8; i = i++; prints 8, and then examines the generated bytecode to reveal the underlying process.

0  bipush 8
2  istore_1
3  iload_1
4  iinc 1 by 1
7  istore_1
8  getstatic #2 <java/lang/System.out>
11 iload_1
12 invokevirtual #3 <java/io/PrintStream.println>
15 return

The article explains each instruction:

bipush pushes the constant 8 onto the operand stack.

istore_1 stores the value into local variable slot 1 (i = 8).

iload_1 loads the value back onto the stack.

iinc 1 by 1 increments the local variable to 9 while the stack still holds 8.

istore_1 stores the stack value (8) back into the local variable, overwriting the incremented value.

The subsequent iload_1 loads the final value (8) for printing.

A diagram (included in the article) visualizes this flow, confirming why the output is 8.

The article then presents a second program (Test2) with i = ++i;. Its bytecode is:

0  bipush 8
2  istore_1
3  iinc 1 by 1
6  iload_1
7  istore_1
8  getstatic #2 <java/lang/System.out>
11 iload_1
12 invokevirtual #3 <java/io/PrintStream.println>
15 return

Here the increment occurs before the value is loaded, so the final stored value is 9, and the program prints 9. The article highlights the key difference: the order of iinc and iload instructions.

Images illustrate the bytecode extraction using the jclasslib plugin and the execution flow diagrams. The conclusion reinforces that understanding JVM bytecode clarifies the seemingly paradoxical behavior of post‑increment versus pre‑increment operators.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMbytecode++iincrement operatori++
Ubiquitous Tech
Written by

Ubiquitous Tech

A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.