Java基本类型占位数总结

本文详细总结了Java中基本数据类型,包括字节数、位数和表示范围,如byte、short、int、long、float、double、char和boolean。特别解析了float和double的内部表示方式,以及boolean类型的特殊存储机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经常把Java基本类型的字节数和位数等一些信息搞混,今天做一个表格,总结一下。

类型字节数位数表示范围
byte1字节8位-128~127 = - 2 7 2^7 27~ 2 7 2^7 27-1
short2字节16位-32768~32767 = - 2 15 2^{15} 215~ 2 15 2^{15} 215-1
int4字节32位-2147483648~2147483647 = - 2 31 2^{31} 231~ 2 31 2^{31} 231-1
long8字节64位- 2 63 2^{63} 263~ 2 63 2^{63} 263-1
float4字节32位-3.4E38~3.4E38
double8字节64位-1.7E308~1.7E308
char2字节16位0 ~ 2 16 2^{16} 216-1
boolean1字节 或4字节true和 false
几点说明

1、1字节=8位(bit);
2、float和double表示范围:
      float类型占32位,其中从高到低分为:1位的符号位;8位的指数位;23位尾数位。
      double类型64位,其中从高到低分为:1位的符号位;11位的指数位;52位的尾数位。
3、char类型占16位,但是无符号位并且可表示中文字符;
4、boolean类型:
      根据官方文档的描述:
      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类型的“大小”并没有精确的给出来。

      但是《Java虚拟机规范》一书中有表明:
      Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. 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.
      虽然Java虚拟机定义了一个布尔类型,但它只为它提供非常有限的支持。 没有Java虚拟机指令专门用于对布尔值的操作。 相反,Java编程语言中对布尔值进行操作的表达式被编译为使用Java虚拟机int数据类型的值
      The Java Virtual Machine does directly support boolean arrays. Its newarray
instruction (§newarray) enables creation of boolean arrays. Arrays of type
boolean are accessed and modified using the byte array instructions baload and
bastore
      Java虚拟机支持boolean数组,可以创建boolean类型数据,当使用boolean数组的时候,允许使用byte数组去存储
      也就是说,当单独使用的时候,以int形式,占4字节存储,当作为boolean数组的时候,就是以byte形式,占1字节去存储;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值