温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中boolean占几字节

发布时间:2021-11-04 13:57:01 来源:亿速云 阅读:472 作者:iii 栏目:编程语言

这篇文章主要介绍“Java中boolean占几字节”,在日常操作中,相信很多人在Java中boolean占几字节问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中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值只有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占几字节”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI