Java Bytecode tutorial
ByteCode is a mechanism for adding platform independence in Java programs. The source code written in the application is converted into java proprietary format known as ByteCode. In fact all jvm languages use byte code which is used on all platforms to execute the application logic.
When we compile the code in. Java file, a. Class file is created which contains the ByteCode which if opened using text editor will show junk characters. We need a hex editor to see the binary data in hexadecimal format. The ByteCode is nothing but proprietary machine instructions in binary format.
Let us see sample code with main method and then see the ByteCode for the same.
public class Test{
public static void main {
System.out. print(''hello') ;
}
}
Using Hex Editor
The corresponding ByteCode for the above program shall be:
ca fe ba be 00 00 00 32 00 22 07 00 02 01 00 10
63 6f 6d 2f 65 78 61 6d 70 6c 65 2f 54 65 73 74
07 00 04 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f
4f 62 6a 65 63 74 01 00 06 3c 69 6e 69 74 3e 01
00 03 28 29 56 01 00 04 43 6f 64 65 0a 00 03 00
09 0c 00 05 00 06 01 00 0f 4c 69 6e 65 4e 75 6d
62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63 61 6c
56 61 72 69 61 62 6c 65 54 61 62 6c 65 01 00 04
74 68 69 73 01 00 12 4c 63 6f 6d 2f 65 78 61 6d
70 6c 65 2f 54 65 73 74 3b 01 00 04 6d 61 69 6e
01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f
53 74 72 69 6e 67 3b 29 56 09 00 11 00 13 07 00
12 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 53 79
73 74 65 6d 0c 00 14 00 15 01 00 03 6f 75 74 01
00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74
53 74 72 65 61 6d 3b 08 00 17 01 00 05 68 65 6c
6c 6f 0a 00 19 00 1b 07 00 1a 01 00 13 6a 61 76
61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d
0c 00 1c 00 1d 01 00 05 70 72 69 6e 74 01 00 15
28 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69
6e 67 3b 29 56 01 00 04 61 72 67 73 01 00 13 5b
4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e
67 3b 01 00 0a 53 6f 75 72 63 65 46 69 6c 65 01
00 09 54 65 73 74 2e 6a 61 76 61 00 21 00 01 00
03 00 00 00 00 00 02 00 01 00 05 00 06 00 01 00
07 00 00 00 2f 00 01 00 01 00 00 00 05 2a b7 00
08 b1 00 00 00 02 00 0a 00 00 00 06 00 01 00 00
00 03 00 0b 00 00 00 0c 00 01 00 00 00 05 00 0c
00 0d 00 00 00 09 00 0e 00 0f 00 01 00 07 00 00
00 37 00 02 00 01 00 00 00 09 b2 00 10 12 16 b6
00 18 b1 00 00 00 02 00 0a 00 00 00 0a 00 02 00
00 00 05 00 08 00 06 00 0b 00 00 00 0c 00 01 00
00 00 09 00 1e 00 1f 00 00 00 01 00 20 00 00 00
02 00 21
Using Java ByteCode Viewer
Apart from using the hex editor to view the hex codes for the byte code, we can also use some bytecode viewer which shows the text version of opcodes and operands and is more human readable. The following is how the above source program will look in a bytecode viewer:
General Information: Major Version: 50 Minor Version: 0 Superclass: java.lang.Object Modifiers: 21 Fields: Methods Information: public <init> ()V max stack: 1, max locals: 1, catch blocks: 0 public static main ([Ljava/lang/String;)V max stack: 2, max locals: 1, catch blocks: 0 Bytecode of main method: getstatic #16 = Field java.lang.System.out(Ljava/io/PrintStream;) ldc #22 = "hello" invokevirtual #24 = Method java.io.PrintStream.print((Ljava/lang/String;)V) return Bytecode of Test() (constructor): aload_0 invokespecial #8 = Method java.lang.Object.<init>(()V) return
Important points to note about bytecode in the above program are:
- Every class starts with the magic hex code “cafebabe”
- Each line contains an opcode which defines the operation to be performed and followed by zero or more operands.
- Bytecode is a set of instructions for the java virtual machine and not the underlying operating system.
- Stack is used to execute the byte code instructions.
- Some of the common opcode operations in terms of bytecode are:
iconst_m1 ——————- pushes int -1 onto the stack
areturn ——————- return back from the current method
imul ——————- Pop top two ints, multiply, push result - The byte code also contains the major and minor version of the .class file which is added by the compiler when we use javac command. This version information acts like a signature and is used at runtime to perform compatibility checks.





