boolean 类型占1个字节的空间

解决方案 »

  1.   

    一个字节,8位吗?
    若是true时,此字节的值为多少?false有时多少?
    为什么不可以像C语言那样,用1和0来表示true和false?
      

  2.   

    不是一个字节,是一位
    true为1
    false为0
      

  3.   

    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.
      

  4.   

    这个跟平台有关吧,有的把它转成byte,int
    即使把他当long ,你又能把他怎么样呢?
    如果和其他的程序通信,如C,我一般是转换成int
    1  true  0 false 
      

  5.   

    boolean:1bit(我查过了),不是1byte
      

  6.   

    为什么?讲来听听!
    java 的逻辑判断又为何不像c/c++那样支持1/ture,0/false??
      

  7.   

    是1bit,c /c++里面没有专门的boolean型数据类型,所以用1表示真,0表示假,
    在java中有独立的boolean型数据,ture表示真,false表示假,ture/false并不是其实际值,只是类似系统中定义的常量.其实际值为1bit.
      

  8.   

    我只知道pascal的boolean变量是一个byte,不是一个bit.因为你不可能只用了一个字节的一位而把其他7位用来储存别的数据,除非是另外7个boolean类型;而即使那样,也会也会大大降低效率
      

  9.   

    下面是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
      

  10.   

    我同意vive所讲的。
    那样的话应该算是 1byte吧!
      

  11.   

    其实具体是1bit或1byte有什么关系呢!
    在机械工业出版社的《Java开发人员指南》(译自Madhu Siddalingaiah的《Java How-To.》)
    中的第4页,明确指出"布尔型数据是1位数据,它的取值可以是True或False。Java的布尔量是一种特殊的数据类型,它与其他数据类型之间不能相互赋值。"