The actrual storage size and memory layout for these data items is not, in fact, required by the language specification.What is specified is the apparent beharior, so ,for example,the effect of bit mast operations,shifts,and so on are entirely predictable at java level.If you write native code,you might find things are different .Importantly, this means that you cannot reliably caculate the amount of memory consumed by adding up data sizes.
下面是sun的SL-275的原话: There are no casts between integer types and the boolean type. Some languages, most notably C and C++, allow numeric values to be interpreted as logical values. This is not permitted in the Java programming language; When a boolean type is required, only boolean values can be used.并没有说长度的问题,似乎boolean的长度是不需要关心的! 但是别的书上明确提出是1bit
若是true时,此字节的值为多少?false有时多少?
为什么不可以像C语言那样,用1和0来表示true和false?
true为1
false为0
即使把他当long ,你又能把他怎么样呢?
如果和其他的程序通信,如C,我一般是转换成int
1 true 0 false
java 的逻辑判断又为何不像c/c++那样支持1/ture,0/false??
在java中有独立的boolean型数据,ture表示真,false表示假,ture/false并不是其实际值,只是类似系统中定义的常量.其实际值为1bit.
There are no casts between integer types and the boolean type. Some languages, most notably C and C++, allow numeric values to be interpreted as logical values. This is not permitted in the Java programming language; When a boolean type is required, only boolean values can be used.并没有说长度的问题,似乎boolean的长度是不需要关心的!
但是别的书上明确提出是1bit
那样的话应该算是 1byte吧!
在机械工业出版社的《Java开发人员指南》(译自Madhu Siddalingaiah的《Java How-To.》)
中的第4页,明确指出"布尔型数据是1位数据,它的取值可以是True或False。Java的布尔量是一种特殊的数据类型,它与其他数据类型之间不能相互赋值。"