Do int Variables Get Cached in the JVM? Exploring Stack, Bytecode, and Constant Pool
This article investigates whether Java int primitives are cached during execution, detailing JVM stack allocation, the specific bytecode instructions (iconst, bipush, sipush, ldc) used for different int ranges, and how local variable tables and operand stacks interact, illustrated with code and bytecode examples.
Int primitive caching in the JVM
Primitive int literals are not universally cached. Whether a value is stored in the constant pool or directly on the operand stack depends on the bytecode instruction used to load it.
Bytecode instructions for loading int constants
iconst_ n – values -1 to 5.
bipush – one‑byte literals –128 to 127.
sipush – two‑byte literals –32768 to 32767.
ldc – values that require a constant‑pool entry (larger than two bytes).
Example:
int i = 5; // iconst_5
int j = -1; // iconst_m1
int k = 127; // bipush 127
int m = 32767; // sipush 32767
int n = 100000; // ldc #constantCorresponding bytecode snippets:
0: iconst_5
1: istore_1
2: iconst_m1
3: istore_2
4: bipush 127
5: istore_3
6: sipush 32767
7: istore_4
8: ldc #100000
9: istore_5Stack frame structure
A stack frame contains a fixed‑size local variable table and an operand stack. Their sizes are recorded in the method’s Code attribute at compile time.
Example class:
public class IntTest {
public void test1() {
int a = 100;
int b = 98;
int c = a + b;
}
}When compiled, the local variable table has slots:
slot 0 – this slot 1 – a slot 2 – b slot 3 – c Execution flow (simplified):
Load a and b from their slots onto the operand stack ( iload_1, iload_2).
Perform addition with iadd.
Store the result back into slot 3 with istore_3.
Bytecode (excerpt) obtained via javap -verbose:
0: aload_0
1: iconst_0
2: istore_1 // a = 100 (iconst or bipush depending on value)
3: iconst_0
4: istore_2 // b = 98
5: iload_1
6: iload_2
7: iadd
8: istore_3 // c = a + bWhen does an int go into the constant pool?
If the literal cannot be represented by iconst, bipush, or sipush, the compiler emits an ldc instruction, which loads the value from the constant pool. Thus only int literals larger than two bytes (outside –32768 ~ 32767) are stored in the constant pool; smaller literals are loaded directly onto the operand stack.
Conclusion
Int primitives are not cached in the same way as string literals. Caching occurs only when the ldc instruction is used, i.e., for values that require a constant‑pool entry. For values within the ranges handled by iconst, bipush, or sipush, the JVM loads the constant directly onto the operand stack without involving the constant pool.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
