boolean类型长度

简介:

true 使用 int 常量 1 表示,false 使用 int 常量 0 表示。 而int在java中是4个字节表示。

建议查看java API官方文档,里面有详细说明:

1)boolean a=true;//这个a在JVM中占4个字节即:32位。
2)boolean[] b = new boolean[10];//数组时,每一个boolean在JVM中占一个字节。
理由:
1)JAVA规范中没有定义boolean类型的大小。
2)但是:在JVM规范第2版中讲得十分清楚。我上边的结论就是从它当中取出来的。
根据:(JVM规范第2版 3.3.4节)
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.  
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. 而:Java virtual machine type int, whose values are 32-bit signed two's-complement integers。
Arrays of type boolean are accessed and modified using the byte array instructions  
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.

PS(请注意最后几句):
sun's Data Types introduction:
byte: The byte data type is an 8-bit signed two's complement integer
short: The short data type is a 16-bit signed two's complement integer
int: The int data type is a 32-bit signed two's complement integer
long: The long data type is a 64-bit signed two's complement integer
float: The float data type is a single-precision 32-bit IEEE 754 floating point
double: The double data type is a double-precision 64-bit IEEE 754 floating point.
char: The char data type is a single 16-bit Unicode character
boolean: The boolean data type has only two possible values: true and false.  
Use this data type for simple flags that track true/false conditions. This data type represents one bit of information,
 but its "size" isn't something that's precisely defined

 



本文转自 zhouhaipeng 51CTO博客,原文链接:http://blog.51cto.com/tianya23/718299,如需转载请自行联系原作者


相关文章
|
2月前
|
SQL 流计算 OceanBase
OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
【2月更文挑战第25天】OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
30 3
|
7月前
|
前端开发 JavaScript
前端基础 - JavaScript值Boolean类型的默认转换
前端基础 - JavaScript值Boolean类型的默认转换
31 0
|
2月前
|
IDE Java 开发工具
阿里巴巴不建议 boolean 类型变量用 isXXX
阿里巴巴不建议 boolean 类型变量用 isXXX
13 1
|
2月前
|
算法 测试技术 编译器
【C++ 基本类型 bool 】深入探索C++中的布尔类型Boolean(二 )
【C++ 基本类型 bool 】深入探索C++中的布尔类型Boolean
29 0
|
2月前
|
程序员 编译器 C语言
【C++ 基本类型 bool 】深入探索C++中的布尔类型Boolean(一)
【C++ 基本类型 bool 】深入探索C++中的布尔类型Boolean
47 0
|
5月前
|
IDE Java 开发工具
阿里巴巴不建议 boolean 类型变量用isXXX的理由?
平时工作中大家经常使用到boolean以及Boolean类型的数据,前者是基本数据类型,后者是包装类,为什么不推荐使用isXXX来命名呢?到底是用基本类型的数据好呢还是用包装类好呢?
|
9月前
|
JavaScript
TypeScript 与 JS 中类型首字母大小写区别(String、string、Number、number、Boolean、boolean ...)
TypeScript 与 JS 中类型首字母大小写区别(String、string、Number、number、Boolean、boolean ...)
175 0
|
JavaScript
js:常见对象的类型判断typeof和布尔值Boolean
js:常见对象的类型判断typeof和布尔值Boolean
84 0
|
存储 Java 容器
Boolean包装类型
博主今天在工作中发现了Boolean包装类型的使用,由此整理了相关内容,下面我们一起来看下吧。
129 0
Boolean包装类型
|
存储 Java
简自动类型提升,精度损失类型强制转换,常用转义字符,简单帮你回顾Java基本数据类型整形浮点型字符型布尔型Boolean及其运算规则
简自动类型提升,精度损失类型强制转换,常用转义字符,简单帮你回顾Java基本数据类型整形浮点型字符型布尔型Boolean及其运算规则
120 1
简自动类型提升,精度损失类型强制转换,常用转义字符,简单帮你回顾Java基本数据类型整形浮点型字符型布尔型Boolean及其运算规则

热门文章

最新文章