在网上看到类似的一个帖子,稍微总结学习一下。字节码详解
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 直接给变量;其它的数据放到常量池中,
该范围的数据在线程内部是可共享的。
字节码详解
分享到:
相关推荐
由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参考: 一、强制类型转换 [java] long ll = 300000; int ...
北京动力节点-Java编程零基础教程-011-Java语言概述-Java的工作原理-源文件及字节码文件.mp4 北京动力节点-Java编程零基础教程-012-Java语言概述-Java的工作原理-JVM分类.mp4 北京动力节点-Java编程零基础教程-...
Java int 转 时间格式
java反向输出int类型整数(不借助char,string和数组)
写一个方法,将int类型转换为字节数组,输入任意int类型整型,输出字节数组;写第二个方法,输入字节数组,输出对应int类型数据。
初学者用于JAVA中int、String的类型转换的几种方法 并说明这其中的区别。
Keil MDK-ARM各种数据类型占用的字节数 char short int float double占用字节数,uCOS-II
java中String类型和int类型比较及相互转换
iconst_m1 将int类型常量-1压入栈 iconst_0 将int类型常量0压入栈 iconst_1 将int类型常量1压入栈 iconst_2 将int类型常量2压入栈 iconst_3 将int类型常量3压入栈 iconst_4 将int类型常量4压入栈 iconst_5 将int类型...
主要讲解Java中基本数据类型,java long 类型与其java int类型的转换的几种方法,希望能给大家做一个参考
所有协议数据的明文需要在尾部加上原始数据长度(int大端字节数4字节,协议数据的长度),再加上“ABCD”4个字母,然后按64位进行分组,不足64位的以“\0”补足,分组后的明文采用统一的加密方式生成密文,即采用 ...
04.java中int占几个字节.avi
JNI开发Java调用C传递int、String、Array类型参数; 详情参考: int类型: (https://blog.csdn.net/niuba123456/article/details/80959892) String类型(https://blog.csdn.net/niuba123456/article/details/80977247);...
提供两个思路,一个是float转int这种。另外就是就是string转int的除法计算。 这只是两个简单的小例子。可以借鉴看看。
Java面试题04.java中int占几个字节.mp4
Java字节数组与常见类型转换工具,包括byte[] 与int long double float string boolean 等类型的相互转换。
下面小编就为大家带来一篇Java 字节数组类型(byte[])与int类型互转方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Java bytes数组与基本类型的相互转换 Int -> Bytes int64ToByte8 int48ToByte6 int40ToByte5 int32ToByte4 int24ToByte3 int16ToByte2 int8ToByte Bytes -> Int bytesToInt64 bytesToInt48 bytesToInt40 bytesTo...
下面小编就为大家带来一篇基于java中byte数组与int类型的转换(两种方法)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了使用Java代码将IP地址转换为int类型的方法,这也是各大计算机考试和ACM以及面试的常见基础问题,需要的朋友可以参考下