
建议查看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版 334节)
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 10 and 11, and the Java 2 SDK, Standard Edition, v12, 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
这两种是有区别的。
先说第一种:
if(this==i) return true, 这只有一种情况 就是当this==i的时候,返回为true,如果不等于的时候呢,就没有返回值,所以方法会报错。
除非,你写成这样,则和第二种方法上在逻辑层面来说并没有太大的一个区别。
if(this==i){
return true;
}else{
return false;
}
再说第二种:return this == i; 返回值可能是true,也可能是false;但不管是哪种返回值,这个方法都能获得方法值,所以方法不会报错。
所以,在没有特殊的要求前提下,建议使用第二种方法,至少代码会更精炼一些。
boolean [] isCovered = new isCovered[99]; //这条语句什么意思?不明白啊。
创建数组 isCovered、数组的长度是99个。。。。。。。。。。。
for就是看里面的;;两个分号中的条件是否满足。
while就是看括号中的条件是否满足。。。。。。。。。。。。。。
建立一个boolean数组,长度10,循环用new Random()nextBoolean()可以随机生成一个 boolean类型值,设置到数组里,然后循环数组统计true和false出现的数量就行了
以上就是关于在java中boolean只能取true,false,它占的空间多大是16位,两个字节吗全部的内容,包括:在java中boolean只能取true,false,它占的空间多大是16位,两个字节吗、JAVA中返回boolean的方法、java布尔值数组问题。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)