JAVA-000

返回

一个简单的 helloworld 小程序,只演示 javac 和 javap 命令行的使用方法。

这是第一个小程序,源代码如下:

package org.example.helloworld;

public class HelloByteCode {
    public static void main(String[] args) {
        HelloByteCode obj = new HelloByteCode();
    }
}

执行命令生成 HelloByteCode.class:

javac HelloByteCode.java

执行命令:

javap -c HelloByteCode.class

结果如下:

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

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class org/example/helloworld/HelloByteCode
       3: dup
       4: invokespecial #3                  // Method "<init>":()V
       7: astore_1
       8: return
}

执行命令:

javap -c -verbose HelloByteCode.class

结果如下:

Classfile /C:/Data/Code/GitHub/GeekbangJavaTrainingCamp/Week_01/Bytecode/bytecode/src/main/java/org/example/helloworld/HelloByteCode.class
  Last modified Oct 16, 2020; size 311 bytes
  MD5 checksum 81bd4153175d3a3bf9e451912c120b58
  Compiled from "HelloByteCode.java"
public class org.example.helloworld.HelloByteCode
  minor version: 0
  major version: 55
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #2                          // org/example/helloworld/HelloByteCode
  super_class: #4                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 2, attributes: 1
Constant pool:
   #1 = Methodref          #4.#13         // java/lang/Object."<init>":()V
   #2 = Class              #14            // org/example/helloworld/HelloByteCode
   #3 = Methodref          #2.#13         // org/example/helloworld/HelloByteCode."<init>":()V
   #4 = Class              #15            // java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Utf8               LineNumberTable
   #9 = Utf8               main
  #10 = Utf8               ([Ljava/lang/String;)V
  #11 = Utf8               SourceFile
  #12 = Utf8               HelloByteCode.java
  #13 = NameAndType        #5:#6          // "<init>":()V
  #14 = Utf8               org/example/helloworld/HelloByteCode
  #15 = Utf8               java/lang/Object
{
  public org.example.helloworld.HelloByteCode();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 3: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=1
         0: new           #2                  // class org/example/helloworld/HelloByteCode
         3: dup
         4: invokespecial #3                  // Method "<init>":()V
         7: astore_1
         8: return
      LineNumberTable:
        line 5: 0
        line 6: 8
}
SourceFile: "HelloByteCode.java"