- Java 9 Programming By Example
- Peter Verhas
- 459字
- 2021-07-02 23:37:27
Looking at the byte code
The class file is a binary file. The main role of this format is to be executed by the JVM and to provide symbolic information for the Java compiler when a code uses some of the classes from a library. When we compile our program that contains System.out.println, the compiler looks at the compiled .class files and not at the source code. It has to find the class named System, the field named out, and the method println. When we debug a piece of code or try to find out why a program does not find a class or method, we will need a way to look into the binary of the .class files. This is not an everyday task and it takes some advanced knowledge
To do so, there is a decompiler that can display the content of a .class file in a more or less readable format. This command is called javap. To execute it, you can issue the following command:
$ javap HelloWorld.class
Compiled from "HelloWorld.java"
public class HelloWorld {
public HelloWorld();
public static void main(java.lang.String[]);
}
The output of the program shows that the class file contains Java class that has something called HelloWorld(); it seems to be a method having the same name as the class and it also contains the method we have written.
The method that has the same name as the class is the constructor of the class. As every class in java can be instantiated, there is a need for a constructor. If we do not give one, then the Java compiler will create one for us. This is the default constructor. The default constructor does nothing special but returns a new instance of the class. If we provide a constructor on our own, then the Java compiler will not have bothered creating one.
The javap decompiler does not show what is inside the methods or what Java code it contains unless we provide the -c option:
$ javap -c HelloWorld.class
Compiled from "HelloWorld.java"
public class HelloWorld {
public HelloWorld();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String hali
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
It is very cryptic and is not for ordinary humans. Only a few experts, who deal with the Java code generation, can fluently read that. But, to have a look at it helps you get a glimpse of what byte code means. It is something like a good old assembly. Although this is binary code, there is nothing secret in it: Java is open source, the class file format is well documented and debuggable for the experts.
- iOS Game Programming Cookbook
- OpenDaylight Cookbook
- Vue.js快速入門與深入實(shí)戰(zhàn)
- 碼上行動:零基礎(chǔ)學(xué)會Python編程(ChatGPT版)
- DevOps入門與實(shí)踐
- Getting Started with CreateJS
- FreeSWITCH 1.6 Cookbook
- 精通Scrapy網(wǎng)絡(luò)爬蟲
- 名師講壇:Java微服務(wù)架構(gòu)實(shí)戰(zhàn)(SpringBoot+SpringCloud+Docker+RabbitMQ)
- QTP自動化測試進(jìn)階
- Windows內(nèi)核編程
- 從零開始學(xué)C語言
- 深入分布式緩存:從原理到實(shí)踐
- Advanced Express Web Application Development
- Java SE實(shí)踐教程