Java 为什么布尔数据类型需要 8 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20948361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:30:04  来源:igfitidea点击:

Why does the boolean data type need 8 bits?

java

提问by imGs

Boolean data type only evaluates to trueor false, so it is always going to take only one bit of memory. So why is there a need of extra 7 bit of memory, isn't it a waste of memory?

布尔数据类型仅计算为trueor false,因此它始终只占用一位内存。那为什么还需要额外的7位内存,岂不是浪费内存?

采纳答案by MariuszS

I think it may need more than 8 bits. It depends on JMV." In Oracle JVM primitive booleanneeds 8 bits, the reason is limited support and lack of optimization.

我认为它可能需要超过 8。这取决于 JMV。”在 Oracle JVM 原语中boolean需要 8 位,原因是支持有限和缺乏优化

Read also: What is the size of a boolean variable in Java?

另请阅读:Java 中布尔变量的大小是多少?

After The Java Tutorials - Primitive Data Types

之后Java教程-基本数据类型

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:boolean 数据类型只有两个可能的值:true 和 false。将此数据类型用于跟踪真/假条件的简单标志。这种数据类型代表一位信息,但它的“大小”并不是精确定义的

After The Java? Virtual Machine Specification

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.

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.

尽管 Java 虚拟机定义了布尔类型,但它只提供了非常有限的支持。没有专用于布尔值操作的 Java 虚拟机指令。相反,Java 编程语言中对布尔值进行操作的表达式被编译为使用 Java 虚拟机 int 数据类型的值。

在 Oracle 的 Java 虚拟机实现中,Java 编程语言中的布尔数组被编码为 Java 虚拟机字节数组,每个布尔元素使用 8 位

For example Booleantype looks in memory like this

例如布尔类型在内存中看起来像这样

header:   8 bytes 
value:    1 byte 
padding:  7 bytes
------------------
sum:      16 bytes

As an alternative to boolean[]you can use for example java.util.BitSet.

作为替代,boolean[]您可以使用例如java.util.BitSet

Why is hard to store booleans as 1 bit? Read Vlad from Moscow answer. You cant address one bit of memory.

为什么很难将布尔值存储为 1 位?阅读来自莫斯科的弗拉德回答。您无法寻址一位内存。

回答by Tim B

You can see a discussion of the actual size used here:

您可以在此处查看有关实际尺寸的讨论:

What is the size of a boolean variable in Java?

Java中布尔变量的大小是多少?

But basically it comes down to a trade off between memory efficiency and performance - especially when you consider that every other variable in the class needs to start word-aligned anyway.

但基本上它归结为内存效率和性能之间的权衡 - 特别是当您考虑到类中的每个其他变量无论如何都需要开始字对齐时。

You can only gain by packing them smaller when you have multiple smaller objects to pack, and then you still need to potentially leave padding to align the next member.

当您有多个较小的对象要打包时,您只能通过将它们打包更小来获得收益,然后您仍然需要潜在地保留填充以对齐下一个成员。

回答by Maroun

See Oracle's Java Virtual Machine implementation:

请参阅Oracle 的 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 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 表示真和 0 表示假对布尔数组组件进行编码。在 Java 编程语言布尔值由编译器映射到 Java 虚拟机类型 int 的值的情况下,编译器必须使用相同的编码。

回答by Vlad from Moscow

It depends of addressability of the memory. The least addressable unit is byte. You can take an address of a byte and do the address arithmetic with it. Moreover there are built-in machine commands that operate with bytes. However it is impossible to take the address of a bit and perform the address arithmetic. In any case at first you have to calculate the address of the byte that contains the target bit and apply additional machine commands that depend of the position of the bit in the byte that to set or reset this bit.

这取决于内存的可寻址性。最小可寻址单位是字节。您可以获取一个字节的地址并用它进行地址算术。此外,还有使用字节操作的内置机器命令。然而,不可能取一个位的地址并执行地址算术。在任何情况下,首先您必须计算包含目标位的字节的地址,并应用取决于字节中位的位置的附加机器命令来设置或重置该位。

回答by mtk

isn't it a waste of memory?

这不是浪费内存吗?

Yes it is a waste of 7 bits, but as everyone of us know a byte is the smallest unit of storage, JVM says it to be of 1 byte.

是的,这浪费了 7 位,但是我们每个人都知道字节是最小的存储单位,JVM 说它是 1 个字节。