新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Java中boolean占几字节-创新互联

这篇文章主要介绍“Java中boolean占几字节”,在日常操作中,相信很多人在Java中boolean占几字节问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中boolean占几字节”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联主营江孜网站建设的网络公司,主营网站建设方案,重庆APP开发公司,江孜h5微信小程序搭建,江孜网站营销推广欢迎江孜等地区企业咨询

这是一个看似简单,实则深究能发现其中一二玄机的问题

让我们来看一下官方文档中的说法

"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值只有true和false两种,这个数据类型只代表1bit的信息,但是它到底占了多少空间,却没有严格的说法,也就是说,不论它的所占空间是多少,只有1bit的信息是有意义的


"In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element."

在 Oracle 的 Java 虚拟机实现中,Java 语言中的 boolean 数组被编码成 Java 虚拟机的 byte 数组,每个元素占 8 比特。

"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 (§baload, §bastore)."

Java 虚拟机中虽然定义了 boolean 类型,但是支持是很有限的,没有专门的虚拟机指令。

同时在 Java 语言中,对 boolean 值的操作被替换成 int 数据类型。

再去看看 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."

“The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false . 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 虚拟机使用 1 表示 true ,0 表示 false 来编码 boolean 数组。

Java 语言的 boolean 值被编译器映射成 Java 虚拟机的 int 类型的时候,也是一样的

到这里,得出一个结论:

boolean到底占几个字节,依赖于JVM

第一,无论Boolean占用多少字节,只有1bit的值是有意义的

第二,boolean 类型被编译成 int 类型来使用,占 4 个 byte 。

第三,在 Java 虚拟机里,1 表示 true ,0 表示 false 。

到此,关于“Java中boolean占几字节”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联-成都网站建设公司网站,小编会继续努力为大家带来更多实用的文章!


本文标题:Java中boolean占几字节-创新互联
标题路径:http://www.wjwzjz.com/article/copgcd.html
在线咨询
服务热线
服务热线:028-86922220
TOP