Java Reference
In-Depth Information
Hello, Bytecode
Normal human beings do not read bytecode. However, Java ships with a tool called “javap” that takes in a
class file and renders its bytecode in a more human-accessible version of the code. There are other tools
for performing this same trick, and if you are interested in getting to know bytecode better, then you should
certainly take a look at the asm toolset. However, javap ships with Oracle's SDK, and that provides all the
functionality that we need while keeping things simple, so we will use it throughout the rest of this chapter.
Let's get a taste of javap and bytecode with a very simple Java class, and its attendant bytecode as
rendered by javap. The class will have a public static void main method that does nothing at all. Given
just that, what is the resulting bytecode? What is generated there will be the baseline for everything that
follows, and will give us an opportunity to see the basic structure of a class file. This is given in Listing 8-1.
Listing 8-1. A Simple Java Class and Its Bytecode
1 // Listing1.java, which is compiled to Listing1.class)
2 public class Listing1 {
3 public static void main(String[] args) {}
4 }
5
6 // Result of executing /usr/bin/javap -v -l -p -s -c Listing1 in the same directory as
Listing1.class
7 Classfile /Users/RCFischer/wkdir/Books/java8/build/classes/production/ch8/Listing1.
class
8 Last modified Dec 6, 2014; size 367 bytes
9 MD5 checksum 008abb234928faafc8a836436ea158db
10 Compiled from "Listing1.java"
11 public class Listing1
12 SourceFile: "Listing1.java"
13 minor version: 0
14 major version: 52
15 flags: ACC_PUBLIC, ACC_SUPER
16 Constant pool:
17 #1 = Methodref #3.#17 // java/lang/Object."<init>":()V
18 #2 = Class #18 // Listing1
19 #3 = Class #19 // java/lang/Object
20 #4 = Utf8 <init>
21 #5 = Utf8 ()V
22 #6 = Utf8 Code
23 #7 = Utf8 LineNumberTable
24 #8 = Utf8 LocalVariableTable
25 #9 = Utf8 this
26 #10 = Utf8 LListing1;
27 #11 = Utf8 main
28 #12 = Utf8 ([Ljava/lang/String;)V
29 #13 = Utf8 args
30 #14 = Utf8 [Ljava/lang/String;
31 #15 = Utf8 SourceFile
32 #16 = Utf8 Listing1.java
33 #17 = NameAndType #4:#5 // "<init>":()V
34 #18 = Utf8 Listing1
35 #19 = Utf8 java/lang/Object
 
Search WWH ::




Custom Search