In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how many bytes are occupied by the boolean type in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is the boolean type
Boolean type: Boolean data types have only two possible values: true and false. Use this data type as a simple tag for tracking true / false conditions. This data type represents this information, but its "size" is not precisely defined.
Stackoverflow has a discussion about how many bytes boolean takes up. One of the highly liked answers from what-is-the-size-of-a-boolean-variable-in-java:
Class LotsOfBooleans {
Boolean a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af
Boolean b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf
Boolean c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf
Boolean d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df
Boolean e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;} class LotsOfInts {
Int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af
Int b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf
Int c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf
Int d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df
Int e0, e1, e2, e3, e4, e5, e6, E7, E8, E9, ea, eb, ec, ed, ee, ef;} public class Test {private static final int SIZE = 1000000; public static void main (String [] args) throws Exception {
LotsOfBooleans [] first = new LotsOfBooleans [SIZE]
LotsOfInts [] second = new LotsOfInts [SIZE]; System.gc ()
Long startMem = getMemory ()
For (int iTuno; I < SIZE; iTunes +) {
First [I] = new LotsOfBooleans ();} System.gc ()
Long endMem = getMemory ()
System.out.println ("Size for LotsOfBooleans:" + (endMem-startMem))
System.out.println ("Average size:" + ((endMem-startMem) / ((double) SIZE); System.gc ()
StartMem = getMemory ()
For (int iTuno; I < SIZE; iTunes +) {
Second [I] = new LotsOfInts ();}
System.gc ()
EndMem = getMemory ()
System.out.println ("Size for LotsOfInts:" + (endMem-startMem))
System.out.println ("Average size:" + ((endMem-startMem) / ((double) SIZE))
/ / Make sure nothing gets collected
Long total = 0
For (int iTuno; I < SIZE; iTunes +) {
Total + = (first.a0? 1: 0) + second.a0;}
System.out.println (total);}
Private static long getMemory () {
Runtime runtime = Runtime.getRuntime ()
Return runtime.totalMemory ()-runtime.freeMemory ();}}
Running result:
Size for LotsOfBooleans: 8257544Average size: 82.57544Size for LotsOfInts: 33599984Average size: 335.99984
The book Java Virtual Machine Specification mentions:
There are no bytecode instructions specific to boolean values in the Java virtual machine, and the Boolean values operated by Java language expressions are replaced by the int data type in the Java virtual machine after compilation.
Java virtual machines directly support arrays of type boolean, which can be created in the newarray section of Chapter 6 for the navarra instructions of virtual machines. Access and modification of the boolean type array share the baload and bastore instructions of the byte type array.
Because it is stated in the virtual machine specification that the Boolean value is replaced by the int data type in the Java virtual machine after compilation, while int is 4 bytes, then the Boolean value is 4 bytes.
The access and modification of the boolean type array share the baload and bastore instructions of the byte type array, because the two are shared, only the two bytes can be common, so a byte in the byte array is 1 byte, then the boolean in the boolean array is 1 byte.
Summary: boolean is 1 byte in the case of an array, and a single boolean is 4 bytes.
The size of the boolean is not explicitly specified in the Java specification. The Java virtual machine specification gives the definition of 4 bytes for a single boolean and 1 byte for the boolean array, depending on whether the virtual machine implementation is in accordance with the specification, so 1 byte and 4 bytes are possible.
This is the end of the content of "how many bytes is occupied by the boolean type in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 281
*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.