简体   繁体   English

Boolean 范围在 Java 编程语言

[英]Boolean range in Java programming language

I found the below paragraph in SCJP 6.0 book.我在 SCJP 6.0 书中找到了以下段落。 What does it mean by the last statement.Which book to read about how these variables are actually stored in memory?.最后一条语句是什么意思。关于这些变量是如何实际存储在memory中的,该阅读哪本书? Thanks a lot.非常感谢。

"That's virtual-machine dependent." “这取决于虚拟机。”

For boolean types there is not a range;对于 boolean 类型,没有范围; a boolean can be only true or false. boolean 只能为真或假。 If someone asks you for the bit depth of a boolean, look them straight in the eye and say, "That's virtual-machine dependent."如果有人问您 boolean 的位深度,请直视他们的眼睛并说:“这取决于虚拟机。” They'll be impressed.他们会印象深刻的。

It means that the only thing required from boolean is to be true or false , no matter what is the underlying implementation.这意味着 boolean 唯一需要的是truefalse ,无论底层实现是什么。

JLS states: JLS指出:

The boolean type has exactly two values: true and false. boolean 类型正好有两个值:真和假。

Where for integral types:整数类型的位置:

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing Unicode characters.整数类型为 byte、short、int 和 long,其值分别为 8 位、16 位、32 位和 64 位有符号二进制补码整数,以及 char,其值为 16 位无符号整数代表 Unicode 个字符。

So you have required bit depth for integral types but it is solely up to you if your boolean will be represented in the memory as a single bit, byte or multibyte variable when you implement your own JVM.因此,您需要整数类型的位深度,但当您实现自己的 Z18B5A217C4DAD256639D7D7ZE 时,您的 boolean 是否将在 memory 中表示为单个位、字节或多字节变量,这完全取决于您。

What is being said is that a VM can implement a boolean as an integer, where a positive value is true and 0 or a negative value is false.所说的是VM可以将boolean实现为integer,其中正值为真,0或负值为假。 The idea is that as long as the VM handles booleans strictly as true or false, it doesn't matter how the VM implements booleans at the OS level.这个想法是,只要 VM 将布尔值严格地处理为 true 或 false,VM 如何在操作系统级别实现布尔值并不重要。

It's entirely virtual machine dependent because they're expecting you to use boolean s sparingly.它完全取决于虚拟机,因为他们希望您谨慎使用boolean Sure you're going to use them all the time, but you're highly unlikely to have more than a handful in a given class.当然,您会一直使用它们,但在给定的 class 中,您极不可能拥有多个。

You are most definiately not going to have something like boolean[] - they made java.util.BitSet for that purpose.你绝对不会有类似boolean[]的东西——他们为此目的制作了java.util.BitSet It just makes more sense to leave boolean s being whatever size they want (8, 16, 32 bits) and keeping it simple than the alternative, which would essentially be a BitSet internally keeping track of all the booleans.boolean保留为他们想要的任何大小(8、16、32 位)并保持简单比替代方案更有意义,这基本上是一个BitSet内部跟踪所有布尔值。 That would've been a poor design decision, imo.那将是一个糟糕的设计决定,imo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM