Java one thousand Q: Do you know the Java language Boolean data accounted for in the end how much space?

As we all know, Java language has a boolean type. Each of the boolean variable is stored in a logical value of true or false. Then store the logical value, you require much space? In theory, this logic value is stored only need a bit (bit) can be a lot of talk about this problem when the textbook, also said boolean types of data only one bit in memory.

But a little common sense knows that a computer: Computer addressing operation is completed when the bytes are the smallest units of. That is to read the data from the memory every time, only the minimum accurate to one byte, you can not read the information on a single bit. If the value of the boolean variable is only 1 bit, 1 byte each time the computer to read information, which contains eight values ​​boolean variables. The computer will have to determine which eight values, which is the value we're looking through some algorithm. This is clearly very unreasonable, because to do this "8-to-1," the operation will increase the operational workload. Then the Java Virtual Machine in the end is how to store boolean value it?

In order to completely clear this problem, we have to tell your planing a plane. Every time a new version of the JDK, Java will release the official version corresponds to a "virtual machine specification." In the "Virtual Machine Specification", there is a special interpretation of boolean type of storage, the paper said:. "While this definition of a boolean data type, but it provides only very limited support is not in any Java Virtual Machine private boolean value for bytecode instructions, boolean expressions in the Java language operation, use the data type int Java virtual machine after compilation instead and boolean array will be encoded into the Java virtual machine byte array , (therefore) accounted for 8 per boolean element. "

According to this argument we can see that: boolean if used alone type of data, 4 bytes of space, and if the stored boolean array, each element has only one byte.
Then the question again: boolean types of data alone, when, why use 4 bytes to store it? With one or two bytes of storage it is more space-saving it? We must know, most of the current 32-bit processors are (not the 32-bit operating system, but to the CPU hardware), the boolean data type is stored as 4 bytes (32-bit) access is the highest efficiency .

Finally, there will be a junior partner asked: "Virtual Machine Specification" is so specified, but in the realization of the virtual machine, really be performed in such rules? To clarify this issue, we have to science a little common sense: the world is not the only company to achieve Oracle Java Virtual Machine (JVM), there are also other companies or organizations publish their own Java virtual machine implementation. These companies or agencies in its own virtual machine, whether or not in full accordance with the provisions of the "norms" to store boolean data type is not known. They will consider the performance of both operational efficiency and storage space.

If you want to learn Java programming system, you can clickhttps://edu.51cto.com/lecturer/2256836.html watch my video lessons on this site, there is a problem you can also add my QQ group to discuss 291,839,907!

Guess you like

Origin blog.51cto.com/2266836/2464732