今天在做题的时候遇到了一个关于boolean占几个字节的问题,带着疑问,随便网上一搜,答案五花八门,在Java中定义的八种基本数据类型中,除了其它七种类型都有明确的内存占用字节数外,就boolean类型没有给出具体的占用字节数。

根据http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html官方文档的描述:

  • byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

  • short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

  • int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigneddivideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

  • long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigneddivideUnsigned etc to support arithmetic operations for unsigned long.

  • float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

  • double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

  • 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.(布尔数据类型只有两个可能的值:true 和 false。将此数据类型用于跟踪真/假条件的简单标志。此数据类型表示一位信息,但其“大小”不是精确定义的内容。)

  • char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

1、1个bit

理由是boolean类型的值只有true和false两种逻辑值,在编译后会使用1和0来表示,这两个数在内存中只需要1位(bit)即可存储,位是计算机最小的存储单位。

2、1个字节

理由是虽然编译后1和0只需占用1位空间,但计算机处理数据的最小单位是1个字节,1个字节等于8位,实际存储的空间是:用1个字节的最低位存储,其他7位用0填补,如果值是true的话则存储的二进制为:0000 0001,如果是false的话则存储的二进制为:0000 0000。

3、4个字节

理由来源是《Java虚拟机规范》一书中的描述:“虽然定义了boolean这种数据类型,但是只对它提供了非常有限的支持。在Java虚拟机中没有任何供boolean值专用的字节码指令,Java语言表达式所操作的boolean值,在编译之后都使用Java虚拟机中的int数据类型来代替,而boolean数组将会被编码成Java虚拟机的byte数组,每个元素boolean元素占8位”。这样我们可以得出boolean类型占了单独使用是4个字节,在数组中又是1个字节。

总结

在《Java虚拟机规范》给出了单个boolean占4个字节,和boolean数组1个字节的定义,具体 还要看虚拟机实现是否按照规范来,所以1个字节、4个字节都是有可能的。

boolean到底占几个字节?相关推荐

  1. JAVA中的char和boolean到底占几个字节?

    对于JAVA中的大部分数据类型,书中都有明确的表示: byte:1字节,short:2字节,int:4字节,long:8字节 float:4字节,double:8字节. 然而对于boolean和cha ...

  2. 一个int类型到底占多少个字节

    一个int占多少个字节? 这个问题我们往往得到的答案是4. 但是int到底占多少个字节,却跟你的机器环境有关. As you can see, the typical data type sizes ...

  3. 一个int类型到底占多少个字节?

    一个int占多少个字节? 这个问题我们往往得到的答案是4. 但是int到底占多少个字节,却跟你的机器环境有关. [html] view plaincopy print? As you can see, ...

  4. java中boolean类型占几个字节

    java的基本数据类型中,boolean只有两种状态,默认值为false.取值范围是{true,false},理论上占1bit,实际上: 1.单个的boolean 类型变量在编译的时候是使用的int ...

  5. 一个指针变量到底占几个字节

    int main() {char ch = 'A';int i = 99;double x = 66.6;char *p = &ch;int *q = &i;double *r = & ...

  6. char在struct中到底占几个字节!!

    struct student {char a;int c;short b;}; printf("%d\n", sizeof(struct student)); 这时char所给分配 ...

  7. 在数据库中一个汉字到底占几个字节

    首先要知道,一个汉字在MySQL中占几个字节是跟编码方式有关的 GBK编码:一个汉字占2个字节 UTF-8编码:一个汉字占3个字节 一般数据库设置的编码格式都是UTF-8,那就以UTF-8为例,var ...

  8. java中char到底占几个字节的问题

    前些天被问到一个问题:java中一个char可以表示一个汉字吗? 我一想char不是占一个字节吗?汉字在utf-8中占三个字节(还有四字节的),char不能够表示!对方一笑,让我后面查查 所以网上搜了 ...

  9. int(11)到底占几个字节,亲测保证可靠

    前言 你是不是一直以为Int(11)就是占11个字节,int(3)就是占3个字节,如果你是这样想的那就大错特错了,这里小编就带着大家一起重新巩固学习吧,打破之前的错误"认为". 开 ...

  10. 整型到底占几个字节呢?

    整型所占的字节数依赖于机器和编译器. 在<深入理解计算机系统>中,展示了两个有代表性的例子:典型的32位机器(作者如是说)和Compaq Alpha体系结构.其中Compaq Alpha是 ...

最新文章

  1. 服务器与ssl证书有关吗,选择网站SSL证书和服务器位置有没有关系和影响
  2. 前端发给后台的参数中含有中文
  3. 后端传前端中文显示都是问好_前后端分离,后台返回的数据前端没法写,怎么办?...
  4. 产品经理的成长历程(宏观)
  5. val_loss突然变很大_女朋友突然变得很冷淡是怎么回事?该怎么办
  6. Python学习笔记之函数(四)
  7. Centos 7 安装 memcached
  8. HDU 2154 跳舞毯
  9. 约瑟夫问题 pku1012(转自奋斗青春(亚伟)blog)
  10. python opencv把一张图片嵌入(叠加)到另一张图片上
  11. 软件生存周期、项目生命周期、产品生命周期区别
  12. 阿里云 Linux 3 安装mysql 亲测有效
  13. ctP2ISP:使用卷积和数据增强的转换器预测蛋白质-蛋白质相互作用位点
  14. 51 Nod1113 矩阵快速幂
  15. day28 re(正则)模块
  16. QGraphicsItem设置绘图区域和鼠标响应以及碰撞检测区域,并实现碰撞检测
  17. IE浏览器消失了?这个软件即可恢复IE浏览器
  18. 开发SpringBoot+Jwt+Vue的前后端分离后台管理系统VueAdmin - 前端笔记
  19. ArgumentError (redundant UTF-8 sequence)
  20. Is there a way to enable UEFI support in KVM

热门文章

  1. mysql sqlyog 1045_sqlyog错误代码1045
  2. 怎么用proteus软件仿真单片机程序?
  3. 如何卸载赛门铁克(Symantec)企业防病毒客户端软件SEP(Symantec Endpoint Protection)?
  4. mac 开启android 模拟器,折腾mac的经验积累 2:mac上如何玩安卓模拟器
  5. testbench实例 vhdl_VHDL的testbench的编写(转)
  6. Ubuntu 定时锁屏改进
  7. Python黑客编程入门教程
  8. 惠普136nw打印机清零_HP惠普打印机清零大全
  9. 台式计算机电源机箱维修,终于理会电脑电源故障的维修方法
  10. 操作系统原理(三)操作系统用户界面