Skip to content

Java Bytecode

Ahmed Yakout edited this page May 6, 2018 · 3 revisions

Java Bytecode

Java bytecode instruction listings

How to generate Byte-code from java code:

javac Main.java
javap -c Main

Example:

 import java.lang.*;
public class Main
{
       	public static void main(String[] arg)
	{
		int i = 1;
		int j = i + 1;
		while (j > 0) {
			j--;
		}

       		// System.out.println("Hello World!");
	}
}

generated bytecode:

Compiled from "Main.java"
public class Main {
  public Main();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iload_1
       3: iconst_1
       4: iadd
       5: istore_2
       6: iload_2
       7: ifle          16
      10: iinc          2, -1
      13: goto          6
      16: return
}
                    <<<=== Unsafe / Safe ===>>>
                                  \
+---------------+        +-------------------+
|  Java source  |   +--> |   Class loader    | --+
+---------------+   |    | Bytecode verifier |   |
        |           |    +-------------------+   |
        V           |             /              |
+---------------+   |             \              V
| Java compiler |  Network        /    +-------------------+
+---------------+   |             \    |      JVM/JIT      |
        |           |             /    +-------------------+
        V           |             \              |
+---------------+   |             /              V
| Java bytecode | --+             \    +-------------------+
+---------------+                 /    | Operating system  |
                                  \    +-------------------+
                                  /              |
                                  \              V
                                  /    +-------------------+
                                  \    |     Hardware      |
                                  /    +-------------------+
                                  \
                    <<<=== Unsafe / Safe ===>>>
Clone this wiki locally