在java中boolean只能取true,false,它占的空间多大?是16位,两个字节吗?
6个回答
展开全部
建议查看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.
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.
展开全部
boolean布尔值,标识是真还是假。
表示布尔值的可以是表达式,也可以是变量。
通过布尔值,你可以判断表达式是否正确或错误。
如 int n = 3;
boolean t = n > 4;
或
boolean t = ((n + 123) / 7) < (n + 64) / 6)
等等
表示布尔值的可以是表达式,也可以是变量。
通过布尔值,你可以判断表达式是否正确或错误。
如 int n = 3;
boolean t = n > 4;
或
boolean t = ((n + 123) / 7) < (n + 64) / 6)
等等
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
boolean布尔值,标识是真还是假。
表示布尔值的可以是表达式,也可以是变量。
通过布尔值,你可以判断表达式是否正确或错误。
如 int n = 3;
boolean t = n > 4;
或
boolean t = ((n + 123) / 7) < (n + 64) / 6)
等等
表示布尔值的可以是表达式,也可以是变量。
通过布尔值,你可以判断表达式是否正确或错误。
如 int n = 3;
boolean t = n > 4;
或
boolean t = ((n + 123) / 7) < (n + 64) / 6)
等等
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当然只能取true和false,占的空间是1个字节,8位
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
只占1位
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询