首页 > 编程语言 >java 字节码指令理解

java 字节码指令理解

时间:2022-08-29 19:12:50浏览次数:44  
标签:ACC java 字节 int Utf8 指令 Test public

public class Test {
    private int num1 = 1;
    public static int NUM1 = 100;
    public int func(int a,int b){
        return add(a,b);
    }
    public int add(int a,int b) {
        return a+b+num1;
    }
    public int sub(int a, int b) {
        return a-b-NUM1;
    }
}




// 生成class文件
D:\>javac -g Test.java

// 查看class详情
D:\>javap -verbose Test.class
Classfile /D:/Test.class
  Last modified 2022年8月29日; size 652 bytes
  MD5 checksum 6b44a7a347f016d2b30fd5e157922e8b
  Compiled from "Test.java"
public class Test
  minor version: 0
  major version: 55
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #5                          // Test
  super_class: #6                         // java/lang/Object
  interfaces: 0, fields: 2, methods: 5, attributes: 1
Constant pool:
   #1 = Methodref          #6.#26         // java/lang/Object."<init>":()V
   #2 = Fieldref           #5.#27         // Test.num1:I
   #3 = Methodref          #5.#28         // Test.add:(II)I
   #4 = Fieldref           #5.#29         // Test.NUM1:I
   #5 = Class              #30            // Test
   #6 = Class              #31            // java/lang/Object
   #7 = Utf8               num1
   #8 = Utf8               I
   #9 = Utf8               NUM1
  #10 = Utf8               <init>
  #11 = Utf8               ()V
  #12 = Utf8               Code
  #13 = Utf8               LineNumberTable
  #14 = Utf8               LocalVariableTable
  #15 = Utf8               this
  #16 = Utf8               LTest;
  #17 = Utf8               func
  #18 = Utf8               (II)I
  #19 = Utf8               a
  #20 = Utf8               b
  #21 = Utf8               add
  #22 = Utf8               sub
  #23 = Utf8               <clinit>
  #24 = Utf8               SourceFile
  #25 = Utf8               Test.java
  #26 = NameAndType        #10:#11        // "<init>":()V
  #27 = NameAndType        #7:#8          // num1:I
  #28 = NameAndType        #21:#18        // add:(II)I
  #29 = NameAndType        #9:#8          // NUM1:I
  #30 = Utf8               Test
  #31 = Utf8               java/lang/Object
{
  public static int NUM1;
    descriptor: I
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC

  public Test();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: iconst_1
         6: putfield      #2                  // Field num1:I
         9: return
      LineNumberTable:
        line 1: 0
        line 2: 4
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      10     0  this   LTest;

  public int func(int, int);
    descriptor: (II)I
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=3, locals=3, args_size=3
         0: aload_0
         1: iload_1
         2: iload_2
         3: invokevirtual #3                  // Method add:(II)I
         6: ireturn
      LineNumberTable:
        line 5: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       7     0  this   LTest;
            0       7     1     a   I
            0       7     2     b   I

  public int add(int, int);
    descriptor: (II)I
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
         0: iload_1
         1: iload_2
         2: iadd
         3: aload_0
         4: getfield      #2                  // Field num1:I
         7: iadd
         8: ireturn
      LineNumberTable:
        line 8: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       9     0  this   LTest;
            0       9     1     a   I
            0       9     2     b   I

  public int sub(int, int);
    descriptor: (II)I
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
         0: iload_1
         1: iload_2
         2: isub
         3: getstatic     #4                  // Field NUM1:I
         6: isub
         7: ireturn
      LineNumberTable:
        line 11: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       8     0  this   LTest;
            0       8     1     a   I
            0       8     2     b   I

  static {};
    descriptor: ()V
    flags: (0x0008) ACC_STATIC
    Code:
      stack=1, locals=0, args_size=0
         0: bipush        100
         2: putstatic     #4                  // Field NUM1:I
         5: return
      LineNumberTable:
        line 3: 0
}
SourceFile: "Test.java"

标签:ACC,java,字节,int,Utf8,指令,Test,public
From: https://www.cnblogs.com/heidsoft/p/16637026.html

相关文章

  • IDEA报错:Error:java: Compilation failed: internal java compiler error
    查看这些配置,确保版本一样......
  • Javaweb-cookiea和session
    Cookie、Session1.会话会话:用户打开了浏览器,打开了多个链接,然后关闭浏览器,这就是一次会话。有状态会话:浏览器怎么证明你来过。服务端和客户端1.服务端给客户端一个......
  • 2022-8-29 javaweb 第一天 servlet/tomcat
    软件架构1、C/S架构:客户端/服务器--------QQ,Typora,腾讯会议。2、B/S架构:浏览器/服务器--------京东,爱奇艺,B站。资源分类静态资源:所有用户访问后,得到的结果都是一......
  • 关于java中system.out.println()中out的理解
    在Java编程中,我们常常用System.out.println()方法来输出,在集合中我经常看到使用方法引用的方式来遍历集合元素:xxx.foreach(System.out::println);也许我们都已经猜到printl......
  • Java设计模式 - 反射机制
    Java设计模式-反射机制......
  • Java实现MD5加密
    Java实现MD5加密1、maven引入jar包<dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.14......
  • java.lang.ArrayStoreException
    List<Object>ol=newArrayList<Object>();ol.add("aaa");/***java.lang.ArrayStoreException*java数组中包括不兼容的值抛出......
  • Java集合
    概述数组和集合的元素存储的个数问题数组定义后类型确定,长度固定集合类型可以不固定,大小是可变的数组和集合存储元素的类型问题基本类型和引用类型的数据数组都......
  • 力扣50(java)-Pow(x,n)(中等)
    题目:实现 pow(x,n) ,即计算x的整数 n次幂函数(即xn)。 示例1:输入:x=2.00000,n=10输出:1024.00000示例2:输入:x=2.10000,n=3输出:9.26100示例3:输入:x=2......
  • js md5 和java md5后的值不一样
         开发发现js对字符串md5和java对字符串md5计算的结果居然不一样,后来找了一个匹配的这里记录一下注:加密的对象中不能有空格,有空格md5后的结果就不一致,都是眼......