In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "boolean occupies a few bytes in Java". In daily operation, I believe that many people have doubts about how many bytes boolean takes in Java. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "boolean accounts for a few bytes in Java". Next, please follow the editor to study!
This is a seemingly simple problem, but in fact, one or two mysteries can be found.
Let's take a look at what is said in the official document.
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. "
It is very clear that there are only two Boolean values, true and false, and this data type only represents the information of 1bit, but there is no strict statement about how much space it takes, that is to say, no matter how much space it occupies, only the information of 1bit is meaningful.
"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."
In the Java virtual machine implementation of Oracle, the boolean array in the Java language is encoded into the byte array of the Java virtual machine, and each element accounts for 8 bits.
"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).
Although the boolean type is defined in the Java virtual machine, the support is very limited, and there are no special virtual machine instructions.
At the same time, in the Java language, operations on boolean values are replaced with int data types.
Let's take a look at what the Java virtual machine specification says.
"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."
The Java virtual machine uses 1 for true and 0 for false to encode the boolean array.
The same is true when the boolean value of the Java language is mapped by the compiler to the int type of the Java virtual machine
Here, come to a conclusion:
How many bytes does boolean take up depends on JVM
First, no matter how many bytes Boolean takes, only the value of 1bit makes sense.
Second, the boolean type is compiled into an int type for use, accounting for 4 byte.
Third, in the Java virtual machine, 1 represents true and 0 represents false.
At this point, the study of "boolean occupies a few bytes in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.