单个boolean使用int来表示,占4个字节,而boolean数组是使用byte数组来表示的
时间:2008-09-28 14:14:50
来源:论坛整理 作者: 编辑:chinaitzhe
我觉得boolean就是一位...
网友回复:具体参考 Java Virtual Machine Specification
http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html
3.3.4 The boolean Type
Although the Java virtual machine defines a boolean type, it only provides very
limited support for it. There are no Java virtual machine instructions solely
dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java
virtual machine int data type.
The Java virtual machine does directly support boolean arrays. Its newarray instruction enables creation of boolean arrays. Arrays of type boolean are accessed and modified
using the byte array instructions baload and bastore(注释:In Sun's JDK
releases 1.0 and 1.1, and the Java 2 SDK, Standard Edition, v1.2, boolean arrays
in the Java programming language are encoded as Java virtual machine byte arrays,
using 8 bits per boolean element.)
The Java virtual machine encodes boolean array components using 1 to represent true and
0 to represent false. Where Java programming language boolean values are mapped by compilers
to values of Java virtual machine type int, the compilers must use the same encoding.
网友回复:也就是说 JVM 对于 boolean 的类型支持非常有限,对于 boolean 来说会被编译成为
一个 int 类型的数据,以后的操作全部采用 int 类型的操作码,使用“1“表示 true,
使用“0”表示 false。
对于 boolean 数组来说,同样使用创建基本类型数组的指令 newarray 来创建 boolean
数组,但它与 byte 数组共用 baload(从数组中读取 byte 或 boolean 值)和 bastore
(将 byte 或 boolean 值存入数组)这两个指令。
因此,对于虚拟机来说根本就不存在 boolean 这个类型。
至于其为什么要这么做,实际上与 JVM 中最基本的数据单元有关,JVM 规范并没有规定一个
数据单元要占多少位,但是最基本的单元必须足够大,至少在一个单元里能放下 byte, short,
int, char, float, 引用类型的值,而用两个单元能放下 long 和 double。因此,JVM 的
实现者至少得选择 32 位的单元作为最小的单元。
同时为了使用 JVM 指令集更小,对于 byte, short, char 这种少于 32 位的类型来说,在
对这些类型进行计算时首先会被转换 int 数据来进行处理的,处理完后再转回去的。
网友回复:学习。
网友回复:
UP
网友回复:火龙果 在j2se上面很深入,学习.
网友回复:就是说没有4个字节以下的基本数据类型了??
网友回复:有啊,byte 占一个字节,而 short 和 char 占两个字节,只不过在运算时
被转换成 int 类型来处理。而对于 boolean 来说,JVM 就没有这种类型。
网友回复:学习..楼上的研究很深入透彻...
网友回复:有啊,byte 占一个字节,而 short 和 char 占两个字节,只不过在运算时
被转换成 int 类型来处理。而对于 boolean 来说,JVM 就没有这种类型。
boolean是怎么样的??等于没说啊,老兄
网友回复:有啊,byte 占一个字节,而 short 和 char 占两个字节,只不过在运算时
被转换成 int 类型来处理。而对于 boolean 来说,JVM 就没有这种类型。
boolean是怎么样的??等于没说啊,老兄
网友回复:mark 学习 不时来看看。
网友回复:
累啊,boolean 是怎么样的,我认为我在 1 楼和 2 楼已经说得很清楚了。
对于单个 boolean 来说采用 int 数据表示,而对于 boolean 数组来说是
使用 byte 数组来表示的。这一切都是编译器的行为,与 JVM 没有任何关
系,也就是说 JVM 中根本不存在 boolean 类型。
网友回复:赞火龙果
网友回复:真佩服火龙果 学的真深入亚 我也要学习
关键字:表示,字节,
上一篇:2009网易笔试一道题目
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面