• Uncategorised
  • 0

Java code to bytecode to constant pool hashes

From Java Code → Bytecode → Constant Pool Hashes



Code Example 🎯

Let’s take the simplest code:

Java Code

public class Demo {
public static void main(String[] args) {
Demo d = new Demo();
String msg = d.sayHello();
System.out.println(msg);
}

public String sayHello() {
return "Hello World";
}
}


Step 1️⃣

Bytecode Generated 🔥

public static void main(java.lang.String[]);
0: new #2 // Create Object Demo
3: dup // Duplicate Reference
4: invokespecial #1 // Call Constructor
7: astore_1 // Store Reference in Local Variable
8: aload_1 // Load Reference
9: invokevirtual #3 // Call sayHello()
12: astore_2 // Store Result in Variable
13: getstatic #4 // Get System.out
16: aload_2 // Load Result
17: invokevirtual #5 // Call println()
20: return


Step 2️⃣

What JVM Sees 🔥

Let’s break this down:

BytecodeMeaningHash
new #2Create Object#2 = Demo Class
dupDuplicate Reference
invokespecial #1Call Constructor#1 = Demo.<init>()
astore_1Store Reference in Local Variable
aload_1Load Reference
invokevirtual #3Call Method#3 = Demo.sayHello()
astore_2Store Result
getstatic #4Get System.out#4 = System.out
aload_2Load Result
invokevirtual #5Call println()#5 = PrintStream.println()
returnMethod End


Step 3️⃣

What are These Hashes?

These hashes represent Constant Pool Table 💪



Constant Pool Table

HashTypeWhat it Points To
#1MethodrefDemo.<init>()V (Constructor)
#2ClassDemo Class
#3MethodrefDemo.sayHello()Ljava/lang/String;
#4FieldrefSystem.out
#5MethodrefPrintStream.println(Ljava/lang/String;)V


Now Let’s Connect Everything 🔥



Line 1

javaCopyEditDemo d = new Demo();

Bytecode:

0: new #2                  // Create Object Demo
3: dup // Duplicate Reference
4: invokespecial #1 // Call Constructor
7: astore_1 // Store Reference

✅ What Happens:

OpcodeHashMeaning
new #2#2Create Object Demo
invokespecial #1#1Call Constructor Demo.<init>()
astore_1Store Reference in Local Variable


Line 2

String msg = d.sayHello();

Bytecode:

asmCopyEdit8: aload_1                 // Load Reference

Line 2️⃣

javaCopyEditString msg = d.sayHello();

Bytecode:

8: aload_1                   // Load reference 'd'
9: invokevirtual #3 // Call sayHello()
12: astore_2 // Store result in variable 'msg'


What happens here?

OpcodeHashMeaning
aload_1Load reference d
invokevirtual #3#3Call method sayHello() from the object
astore_2Store the result in msg


How JVM Resolves this?

👉 invokevirtual #3 checks the Constant Pool Table

Constant Pool Entry for #3

#3 = Methodref Demo.sayHello()Ljava/lang/String;

✅ This means:

  • Class = Demo
  • Method = sayHello()
  • Return Type = String


Line 3️⃣

javaCopyEditSystem.out.println(msg);

Bytecode:

13: getstatic     #4                 // Get System.out
16: aload_2 // Load result (msg)
17: invokevirtual #5 // Call println()
20: return


What happens here?

OpcodeHashMeaning
getstatic #4#4Get System.out
aload_2Load msg
invokevirtual #5#5Call println() method


Constant Pool Entry for #4

#4 = Fieldref java/lang/System.out Ljava/io/PrintStream;

✅ This means:

  • Class = System
  • Field = out
  • Type = PrintStream


Constant Pool Entry for #5

#5 = Methodref java/io/PrintStream.println(Ljava/lang/String;)V

✅ This means:

  • Class = PrintStream
  • Method = println
  • Argument = String
  • Return = void


Now Bhai 🧠💪

Let’s connect ALL HASHES 🔥🔥

BytecodeHashConstant Pool Entry
new #2#2Class -> Demo
invokespecial #1#1Method -> Demo.<init>()
invokevirtual #3#3Method -> Demo.sayHello()
getstatic #4#4Field -> System.out
invokevirtual #5#5Method -> PrintStream.println()


💥 Final Picture

Demo d = new Demo();        → new #2 → invokespecial #1
String msg = d.sayHello(); → invokevirtual #3
System.out.println(msg); → getstatic #4 → invokevirtual #5


Now Coming to Your Million Dollar Question 💰💪

🔥 Why This Constant Pool Exists?

✅ Because in JVM, bytecode must be small and compact
Instead of storing names like "sayHello" or "println" everywhere,
it stores just the HASH Reference Numbers.



You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *