`
xiaomogui
  • 浏览: 239132 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java基础之int类型字节码初识

阅读更多

在网上看到类似的一个帖子,稍微总结学习一下。字节码详解

 

package com.test;

public class Test1 {

	public void test1() {
		int a = 1;
		int b = 2;
		int c = -1;
		int d = -2;

	}

	public void test2() {
		int a = 5;
		int b = 6;
		int c = -5;
		int d = -6;
	}

	public void test3() {
		int a = 32767;
		int b = 32768;
		int c = -32768;
		int d = -32769;
	}
	
	public void test4() {
		int a = 65535;
		int b = 65536;
		int c = 65536;
		int d = -65535;
		int e = 65536;
	}

}

 

 

Compiled from "Test1.java"
public class com.test.Test1 extends java.lang.Object{
public com.test.Test1();
  Code:
   0: aload_0
   1: invokespecial #8; //Method java/lang/Object."<init>":()V
   4: return

public void test1();
  Code:
   0: iconst_1
   1: istore_1
   2: iconst_2
   3: istore_2
   4: iconst_m1
   5: istore_3
   6: bipush -2
   8: istore 4
   10: return

public void test2();
  Code:
   0: iconst_5
   1: istore_1
   2: bipush 6
   4: istore_2
   5: bipush -5
   7: istore_3
   8: bipush -6
   10: istore 4
   12: return

public void test3();
  Code:
   0: sipush 32767
   3: istore_1
   4: ldc #22; //int 32768
   6: istore_2
   7: sipush -32768
   10: istore_3
   11: ldc #23; //int -32769
   13: istore 4
   15: return

public void test4();
  Code:
   0: ldc #25; //int 65535
   2: istore_1
   3: ldc #26; //int 65536
   5: istore_2
   6: ldc #26; //int 65536
   8: istore_3
   9: ldc #27; //int -65535
   11: istore 4
   13: ldc #26; //int 65536
   15: istore 5
   17: return

}

 
public void test1();
  Code:
   0: iconst_1
   // 操作数栈 压入1
   1: istore_1
   // 把操作数栈栈顶值弹出,存入栈帧中的局部变量第一个数组中
   2: iconst_2
   // 操作数栈 压入2
   3: istore_2
   // 把操作数栈栈顶值弹出,存入栈帧中的局部变量第二个数组中
   4: iconst_m1
   //  将int型-1 压入至栈顶
   5: istore_3
   // 把操作数栈栈顶值弹出,存入栈帧中的局部变量第三个数组中
   6: bipush -2
   //  将int型-2 压入至栈顶
   8: istore 4
   // 把操作数栈栈顶值弹出,存入栈帧中的局部变量第三个数组中
   10: return
   // 方法执行完成 ,返回
a,b,c和d是直接从指令获取数值,而没有进行栈中交换或进入常量池。

 

public void test2();
  Code:
   0: iconst_5
   //  将int型5 压入至栈顶
   1: istore_1
   // 把操作数栈栈顶值弹出,存入栈帧中的局部变量第一个数组中
   2: bipush 6
   // 将单字节的常量值(-128~127)6压入至栈顶,直接给变量。
   4: istore_2
   // 把操作数栈栈顶值弹出,存入栈帧中的局部变量第二个数组中
   5: bipush -5
   7: istore_3
   8: bipush -6
   10: istore 4
   12: return
JAVA虚拟机对常用常量(比如0,1,2,3,4,5)的操作直接定义成了指令,而不是传统的操作指令后带操作数。 目的是减少指令长度。没有iconst_6的指令! 而是bipush 6,就是传统的指令+操作数。
a,b,c和d是直接直接给变量

 

public void test3();
  Code:
   0: sipush 32767
   //  将一个短整型常量值(-32768~32767)压入到栈顶,32767占用两个字节,sipush命令栈用
   //  一个字节,所有istore_1从3开始。

   3: istore_1
   // 把操作数栈栈顶值弹出,存入栈帧中的局部变量第一个数组中
   4: ldc #22; //int 32768
   // 从常量池中找到数组索引为22的数据,并把该值压入到栈顶栈中
   6: istore_2
   7: sipush -32768
   10: istore_3
   11: ldc #23; //int -32769
   13: istore 4
   15: return

 

public void test4();
  Code:
   0: ldc #25; //int 65535
   2: istore_1
   3: ldc #26; //int 65536
   // 从常量池中找到数组索引为26的数据,并把该值压入到栈顶栈中
   5: istore_2
   6: ldc #26; //int 65536
   // 从常量池中找到数组索引为26的数据,并把该值压入到栈顶栈中
   8: istore_3
   9: ldc #27; //int -65535
   11: istore 4
   13: ldc #26; //int 65536
   15: istore 5
   17: return

}


对于65536,大于两个字节的,编译的时候把它放入常量池部分,而把取这个数的指令写为#26,所以说
#26是线程内部可以共享的。
好处是减少了指令代码的长度.这个值可以被多次使用。


所以对于int类型的数据,0-5 直接从指令中得到;-32768~32767 直接给变量;其它的数据放到常量池中,
该范围的数据在线程内部是可共享的。


字节码详解

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics