首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

a Class constant pool 分析

1、源代码

package Linking.ex5;

public class Salutation {

//** 这些都是类属性(数据)

private static final String hello="Hello,world!";

private static final String greeting="Greetings,planet!";

private static final String saulutation="Saulutations,orb!";

private static int choice=(int)(Math.random()*2.99);

public static void main(String[] args) {

String s=hello;//建立新的字符串对象。

s.intern();

if (choice==1)

s=greeting;

else if (choice==2)

s=saulutation;

int i=5;

Integer i1=new Integer(i);

}

}

2、Salutation.class 编译后的字节码,常量池+一条一条指令。

常量池举例:

常量池一条记录有两数据:1.序列号 2.值

1)方法Math.random():1.属于类Math,方法名字random,类型:返回类型 Double:()D

private static int choice=(int)(Math.random()*2.99);

下面就是class的常量池数据:当jvm装入class文件进入内存,符号#23里面的值替换成Math类的真正地址,#26里面的值也会替换成值得真正地址,所以现在静态class存放的是符号连接。

2)#27 = Double 2.99d

3)变量名choice:1.属于哪个类,变量名,变量类型I

4)方法 :()V ,返回值void。

这一语句:(int)(Math.random()*2.99)系统默认的方法。

3、显示.class 文件

javap -c -v -constants -v -l Salutation.class

public class Linking.ex5.Salutation

minor version: 0

major version: 51

flags: ACC_PUBLIC, ACC_SUPER

Constant pool:

序号 值

#1 = Class #2 // Linking/ex5/Salutation

#2 = Utf8 Linking/ex5/Salutation

#3 = Class #4 // java/lang/Object

#4 = Utf8 java/lang/Object //表示此类父类

#5 = Utf8 hello //成员变量

#6 = Utf8 Ljava/lang/String;//类成员类型

#7 = Utf8 ConstantValue: static final 的字符串是常量

#8 = String #9 // Hello,world! ,类成员值得类型

#9 = Utf8 Hello,world!//类成员值

#10 = Utf8 greeting//类成员类型

#11 = String #12 // Greetings,planet!

#12 = Utf8 Greetings,planet!

#13 = Utf8 saulutation

#14 = String #15 // Saulutations,orb!

#15 = Utf8 Saulutations,orb!

#16 = Utf8 choice

#17 = Utf8 I

#18 = Utf8

#19 = Utf8 ()V:表示返回类型是void

#20 = Utf8 Code

#21 = Methodref #22.#24 // java/lang/Math.random:()D

#22 = Class #23 // java/lang/Math

#23 = Utf8 java/lang/Math

#24 = NameAndType #25:#26 // random:()D

#25 = Utf8 random

#26 = Utf8 ()D

#27 = Double 2.99d

#29 = Fieldref #1.#30 // Linking/ex5/Salutation.choice:I

#30 = NameAndType #16:#17 // choice:I

#31 = Utf8 LineNumberTable

#32 = Utf8 LocalVariableTable

#33 = Utf8

#34 = Methodref #3.#35 // java/lang/Object."":()V

#35 = NameAndType #33:#19 // "":()V

#36 = Utf8 this

#37 = Utf8 LLinking/ex5/Salutation;

#38 = Utf8 main

#39 = Utf8 ([Ljava/lang/String;)V

#40 = Methodref #41.#43 // java/lang/String.intern:()Ljava/lang/String;

#41 = Class #42 // java/lang/String

#42 = Utf8 java/lang/String

#43 = NameAndType #44:#45 // intern:()Ljava/lang/String;

#44 = Utf8 intern

#45 = Utf8 ()Ljava/lang/String;

#46 = Fieldref #47.#49 // java/lang/System.out:Ljava/io/PrintStream;

#47 = Class #48 // java/lang/System

#48 = Utf8 java/lang/System

#49 = NameAndType #50:#51 // out:Ljava/io/PrintStream;

#50 = Utf8 out

#51 = Utf8 Ljava/io/PrintStream;

#52 = Class #53 // java/lang/StringBuilder

#53 = Utf8 java/lang/StringBuilder

#54 = String #55 // choice:

#55 = Utf8 choice:

#56 = Methodref #52.#57 // java/lang/StringBuilder."":(Ljava/lang/String;)V

#57 = NameAndType #33:#58 // "":(Ljava/lang/String;)V

#58 = Utf8 (Ljava/lang/String;)V

#59 = Methodref #52.#60 // java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;

#60 = NameAndType #61:#62 // append:(I)Ljava/lang/StringBuilder;

#61 = Utf8 append

#62 = Utf8 (I)Ljava/lang/StringBuilder;

#63 = Methodref #52.#64 // java/lang/StringBuilder.toString:()Ljava/lang/String;

#64 = NameAndType #65:#45 // toString:()Ljava/lang/String;

#65 = Utf8 toString

#66 = Methodref #67.#69 // java/io/PrintStream.print:(Ljava/lang/String;)V

#67 = Class #68 // java/io/PrintStream

#68 = Utf8 java/io/PrintStream

#69 = NameAndType #70:#58 // print:(Ljava/lang/String;)V

#70 = Utf8 print

#71 = Class #72 // java/lang/Integer

#72 = Utf8 java/lang/Integer

#73 = Methodref #71.#74 // java/lang/Integer."":(I)V

#74 = NameAndType #33:#75 // "":(I)V

#75 = Utf8 (I)V

#76 = Utf8 args

#77 = Utf8 [Ljava/lang/String;

#78 = Utf8 s

#79 = Utf8 i

#80 = Utf8 i1

#81 = Utf8 Ljava/lang/Integer;

#82 = Utf8 StackMapTable

#83 = Utf8 SourceFile

#84 = Utf8 Salutation.java

{

static {};

descriptor: ()V

flags: ACC_STATIC

Code:

stack=4, locals=0, args_size=0

0: invokestatic #21 // Method java/lang/Math.random:()D

3: ldc2_w #27 // double 2.99d

6: dmul

7: d2i

8: putstatic #29 // Field choice:I

11: return

LineNumberTable:

line 8: 0

LocalVariableTable:

Start Length Slot Name Signature

public Linking.ex5.Salutation();

descriptor: ()V

flags: ACC_PUBLIC

Code:

stack=1, locals=1, args_size=1

0: aload_0

1: invokespecial #34 // Method java/lang/Object."":()V

4: return

LineNumberTable:

line 3: 0

LocalVariableTable:

Start Length Slot Name Signature

0 5 0 this LLinking/ex5/Salutation;

public static void main(java.lang.String[]);

descriptor: ([Ljava/lang/String;)V

flags: ACC_PUBLIC, ACC_STATIC

Code:

stack=4, locals=4, args_size=1

0: ldc #8 // String Hello,world!

2: astore_1

3: aload_1

4: invokevirtual #40 // Method java/lang/String.intern:()Ljava/lang/String;

7: pop

8: getstatic #46 // Field java/lang/System.out:Ljava/io/PrintStream;

11: new #52 // class java/lang/StringBuilder

14: dup

15: ldc #54 // String choice:

17: invokespecial #56 // Method java/lang/StringBuilder."":(Ljava/lang/String;)V

20: getstatic #29 // Field choice:I

23: invokevirtual #59 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;

26: invokevirtual #63 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;

29: invokevirtual #66 // Method java/io/PrintStream.print:(Ljava/lang/String;)V

32: getstatic #29 // Field choice:I

35: iconst_1

36: if_icmpne 45

39: ldc #11 // String Greetings,planet!

41: astore_1

42: goto 55

45: getstatic #29 // Field choice:I

48: iconst_2

49: if_icmpne 55

52: ldc #14 // String Saulutations,orb!

54: astore_1

55: getstatic #46 // Field java/lang/System.out:Ljava/io/PrintStream;

58: aload_1

59: invokevirtual #66 // Method java/io/PrintStream.print:(Ljava/lang/String;)V

62: iconst_5

63: istore_2

64: new #71 // class java/lang/Integer

67: dup

68: iload_2

69: invokespecial #73 // Method java/lang/Integer."":(I)V

72: astore_3

73: return

LineNumberTable:

line 17: 0

line 18: 3

line 19: 8

line 20: 32

line 21: 39

line 22: 45

line 23: 52

line 24: 55

line 25: 62

line 26: 64

line 27: 73

LocalVariableTable:

Start Length Slot Name Signature

0 74 0 args [Ljava/lang/String;

3 71 1 s Ljava/lang/String;

64 10 2 i I

73 1 3 i1 Ljava/lang/Integer;

StackMapTable: number_of_entries = 2

frame_type = 252 /* append */

offset_delta = 45

locals = [ class java/lang/String ]

frame_type = 9 /* same */

}

SourceFile: "Salutation.java"

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180304G18GDF00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券