Read bytecode with javap
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)You wrote simple Java, but you want to see what the compiler really produced, for example how a string + or a loop is compiled. The .class file is unreadable in a text editor. What do you use?
javap -c: the bytecode of each method
javap is a JDK tool that shows what is inside a .class file, including its methods and, with -c, the bytecode instructions.
javap -c ClassName prints each method's signature followed by its bytecode. You run it on compiled classes, so compile first; then give a class name (javap -c Wallet) or a path to a class file (javap -c out/Wallet.class).
Example 1 · The class we will disassemble
public class Wallet { private static final int DAILY_LIMIT = 5000; private int balance;
public Wallet(int balance) { this.balance = balance; }
public boolean pay(int amount) { if (amount > balance || amount > DAILY_LIMIT) { return false; } balance = balance - amount; log("Payment done"); return true; }
private void log(String message) { System.out.println(message); }
public static int fee(String upiId) { return upiId.endsWith("@bank") ? 0 : 2; }
public static void main(String[] args) { Wallet w = new Wallet(1200); System.out.println(w.pay(300)); System.out.println(w.pay(2000)); System.out.println("Fee: " + fee("riya@bank")); }}Payment done true false Fee: 0
What this code does
- A small UPI-style wallet: a constant, a private field, a constructor, a public method, a private method and a static method. Each one shows up differently in javap.
pay(300)succeeds and prints the log line;pay(2000)fails because 2000 is more than the balance of 900 left after the first payment.
Example 2 · javap -c Wallet
javac --release 17 Wallet.javajavap -c WalletCompiled from "Wallet.java"
public class Wallet {
public Wallet(int);
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iload_1
6: putfield #7 // Field balance:I
9: return
public boolean pay(int);
Code:
0: iload_1
1: aload_0
2: getfield #7 // Field balance:I
5: if_icmpgt 15
8: iload_1
9: sipush 5000
12: if_icmple 17
15: iconst_0
16: ireturn
17: aload_0
18: aload_0
19: getfield #7 // Field balance:I
22: iload_1
23: isub
24: putfield #7 // Field balance:I
27: aload_0
28: ldc #13 // String Payment done
30: invokevirtual #15 // Method log:(Ljava/lang/String;)V
33: iconst_1
34: ireturn
public static int fee(java.lang.String);
Code:
0: aload_0
1: ldc #30 // String @bank
3: invokevirtual #32 // Method java/lang/String.endsWith:(Ljava/lang/String;)Z
6: ifeq 13
9: iconst_0
10: goto 14
13: iconst_2
14: ireturn
...
}What this code does
- The constructor
Wallet(int)first callsObject.<init>(every constructor calls its parent's), thenputfieldstores the argument intobalance.<init>is the bytecode name of a constructor. DAILY_LIMITnever appears as a field read: it is a compile-time constant, so javac copied5000straight into the code (sipush 5000). The||became two jumps,if_icmpgtandif_icmple, andlogis called withinvokevirtual.- Offsets jump by different amounts because instructions have different sizes:
iload_1is 1 byte,getfield #7is 3 bytes.
It works on JDK classes too: javap -c java.lang.Math. Every bytecode line has the same shape. In 2: getfield #7 // Field balance:I, 2 is the offset (the byte position inside the method), getfield is the opcode (the instruction), #7 is an operand pointing into the constant pool, and the part after // is javap's comment saying what #7 means.
Without other options, javap hides private members. The private method log below is called by pay, yet it does not appear in the listing.
javap Wallet.java fails with Error: class not found: Wallet.java. javap reads class files, not source files.javap -p and javap -v
javap -p shows all members, including private fields and methods. Combine options freely: javap -c -p Wallet prints the bytecode of private methods too.
Example 3 · javap -p and an excerpt of javap -v
javap -p Walletjavap -v -p Wallet$ javap -p Wallet
Compiled from "Wallet.java"
public class Wallet {
private static final int DAILY_LIMIT;
private int balance;
public Wallet(int);
public boolean pay(int);
private void log(java.lang.String);
public static int fee(java.lang.String);
public static void main(java.lang.String[]);
}
$ javap -v -p Wallet
Classfile /.../Wallet.class
Last modified 15-Oct-2026; size 1441 bytes
SHA-256 checksum 50ad6b63142415c8020f82965009a992ce32dc91eb677f06eede2063cb933f7f
Compiled from "Wallet.java"
public class Wallet
minor version: 0
major version: 61
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
this_class: #8 // Wallet
super_class: #2 // java/lang/Object
interfaces: 0, fields: 2, methods: 5, attributes: 3
Constant pool:
#1 = Methodref #2.#3 // java/lang/Object."<init>":()V
#2 = Class #4 // java/lang/Object
#3 = NameAndType #5:#6 // "<init>":()V
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Fieldref #8.#9 // Wallet.balance:I
#8 = Class #10 // Wallet
#9 = NameAndType #11:#12 // balance:I
#10 = Utf8 Wallet
#11 = Utf8 balance
#12 = Utf8 I
#13 = String #14 // Payment done
#14 = Utf8 Payment done
#15 = Methodref #8.#16 // Wallet.log:(Ljava/lang/String;)V
#16 = NameAndType #17:#18 // log:(Ljava/lang/String;)V
...
{
...
public boolean pay(int);
descriptor: (I)Z
flags: (0x0001) ACC_PUBLIC
Code:
stack=3, locals=2, args_size=2
0: iload_1
1: aload_0
2: getfield #7 // Field balance:I
5: if_icmpgt 15
8: iload_1
9: sipush 5000
12: if_icmple 17
15: iconst_0
16: ireturn
17: aload_0
18: aload_0
19: getfield #7 // Field balance:I
22: iload_1
23: isub
24: putfield #7 // Field balance:I
27: aload_0
28: ldc #13 // String Payment done
30: invokevirtual #15 // Method log:(Ljava/lang/String;)V
33: iconst_1
34: ireturn
LineNumberTable:
line 10: 0
line 11: 15
line 13: 17
line 14: 27
line 15: 33
StackMapTable: number_of_entries = 2
frame_type = 15 /* same */
frame_type = 1 /* same */
...
}What this code does
-padds the private fieldbalance, the private constantDAILY_LIMITand the private methodlogthat plainjavaphid.-vadds the header (version 61, flags), the constant pool and per-method details. Entry#13is the stringPayment done, whichldc #13inpayloads;#15is the method referenceWallet.log:(Ljava/lang/String;)V.StackMapTablelists type information at jump targets. The JVM's verifier uses it to check the bytecode quickly; you can ignore it when reading code.
javap -v (verbose) prints everything: the class file version, flags, the full constant pool, and for each method its descriptor, flags, stack and local sizes, bytecode and tables. It is long, so use it when you need the constant pool or the exact numbers. In -v output, stack=3, locals=2, args_size=2 says the method needs at most 3 values on its operand stack and 2 local variable slots, and receives 2 arguments. For an instance method the hidden this counts as an argument, so pay(int) has args_size=2.
Method descriptors
A method descriptor is the compact type of a method inside a class file: parameter types in brackets, then the return type. (I)V takes an int and returns void; (Ljava/lang/String;)I takes a String and returns an int.
Example 4 · Print descriptors from Java code
import java.lang.invoke.MethodType;import java.lang.reflect.Method;import java.util.Arrays;import java.util.Comparator;import java.util.List;
class Shop { void addItem(int price) { } int countItems(String category) { return 0; } boolean canPay(long walletPaise, double billRupees) { return true; } String[] names(char section) { return new String[0]; } List<String> orders(int[] tableNumbers) { return List.of(); }}
public class DescriptorDemo { public static void main(String[] args) { Method[] methods = Shop.class.getDeclaredMethods(); Arrays.sort(methods, Comparator.comparing(Method::getName)); for (Method m : methods) { MethodType type = MethodType.methodType(m.getReturnType(), m.getParameterTypes()); System.out.printf("%-10s %s%n", m.getName(), type.toMethodDescriptorString()); } System.out.println("String[] as a type: " + String[].class.descriptorString()); }}addItem (I)V canPay (JD)Z countItems (Ljava/lang/String;)I names (C)[Ljava/lang/String; orders ([I)Ljava/util/List; String[] as a type: [Ljava/lang/String;
What this code does
MethodType.toMethodDescriptorString()builds the same descriptor javac writes into the class file.Class.descriptorString()(Java 12) gives the descriptor of a single type.canPay(long, double)is(JD)Z:Jfor long andZfor boolean are the two letters people mix up.ordersreturnsList<String>, but the descriptor keeps only the erasedLjava/util/List;.
Example 5 · javap -s -p Shop
javap -s -p ShopCompiled from "DescriptorDemo.java"
class Shop {
Shop();
descriptor: ()V
void addItem(int);
descriptor: (I)V
int countItems(java.lang.String);
descriptor: (Ljava/lang/String;)I
boolean canPay(long, double);
descriptor: (JD)Z
java.lang.String[] names(char);
descriptor: (C)[Ljava/lang/String;
java.util.List<java.lang.String> orders(int[]);
descriptor: ([I)Ljava/util/List;
}What this code does
-sprints the descriptor under each member, so you can check your decoding of the previous example. The default constructorShop()is()V.- The Java-style line above each descriptor still shows
List<String>: javap reads the generic type from theSignatureattribute, while the descriptor holds only the erased type.
Primitive types are single letters. Class types are L, the class name with slashes, and ;. Arrays add [ in front: [I is int[] and [Ljava/lang/String; is String[]. There are no spaces or commas, so (JD)Z takes a long and a double and returns a boolean. Descriptors use erased types: List<String> is just Ljava/util/List;. javap -s prints the descriptor of every member.
Bbyte,Cchar,Sshort,Iint,Jlong (not L),Ffloat,Ddouble,Zboolean (not B),VvoidLjava/lang/String;String,Ljava/util/List;any List[Iint[],[[Ddouble[][],[Ljava/lang/String;String[]
javap -l: line numbers and local variables
javap -l prints two tables per method. The LineNumberTable maps bytecode offsets to source lines: line 13: 17 means the code for line 13 starts at offset 17. Stack traces use this table to show Wallet.java:13.
Example 6 · javap -l without and with -g
javac --release 17 Wallet.javajavap -l Walletjavac -g --release 17 Wallet.javajavap -l Wallet$ javac --release 17 Wallet.java
$ javap -l Wallet
...
public boolean pay(int);
LineNumberTable:
line 10: 0
line 11: 15
line 13: 17
line 14: 27
line 15: 33
...
$ javac -g --release 17 Wallet.java
$ javap -l Wallet
...
public boolean pay(int);
LineNumberTable:
line 10: 0
line 11: 15
line 13: 17
line 14: 27
line 15: 33
LocalVariableTable:
Start Length Slot Name Signature
0 35 0 this LWallet;
0 35 1 amount I
public static int fee(java.lang.String);
LineNumberTable:
line 23: 0
LocalVariableTable:
Start Length Slot Name Signature
0 15 0 upiId Ljava/lang/String;
...What this code does
- Both builds have a LineNumberTable:
line 13: 17matches offset 17 in thejavap -clisting, wherebalance = balance - amountstarts. - Only the
-gbuild has a LocalVariableTable. Slot 0 ofpayisthisand slot 1 isamount; in staticfee, slot 0 is the first parameterupiId.StartandLengthgive the offsets where each variable exists.
The LocalVariableTable gives each local variable slot a name and type. javac writes it only when you compile with -g, because it is debugging information. Without it, debuggers and javap see slots by number only. By default javac includes line numbers and the source file name but not local variable names. That is why stack traces from normal builds still have line numbers.
Next step
Check your understanding
11 quick questions