当前位置:网站首页>How many bytes does Boolean occupy?
How many bytes does Boolean occupy?
2022-07-25 22:08:00 【l'amour Хэ рутилин】
I met a question about boolean The problem of occupying a few bytes , With questions , Search the Internet , There are many answers , stay Java Of the eight basic data types defined in , Except that the other seven types have a clear number of bytes of memory , Just boolean The type does not give a specific number of occupied bytes .
according to http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html Description of official documents :
byte: The
bytedata type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). Thebytedata type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place ofintwhere their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.short: The
shortdata type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As withbyte, the same guidelines apply: you can use ashortto save memory in large arrays, in situations where the memory savings actually matters.int: By default, the
intdata type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use theintdata type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to useintdata type as an unsigned integer. See the section The Number Classes for more information. Static methods likecompareUnsigned,divideUnsignedetc have been added to the Integer class to support the arithmetic operations for unsigned integers.long: The
longdata type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use thelongdata type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided byint. The Long class also contains methods likecompareUnsigned,divideUnsignedetc to support arithmetic operations for unsigned long.float: The
floatdata type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations forbyteandshort, use afloat(instead ofdouble) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings coversBigDecimaland other useful classes provided by the Java platform.double: The
doubledata type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.boolean: The
booleandata type has only two possible values:trueandfalse. 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 data types have only two possible values :true and false. Use this data type to track true / Simple sign of false condition . This data type represents a bit of information , But it's “ size ” Not precisely defined .)char: The
chardata type is a single 16-bit Unicode character. It has a minimum value of'\u0000'(or 0) and a maximum value of'\uffff'(or 65,535 inclusive).
1、1 individual bit
The reason is that boolean The value of type is only true and false Two logical values , It will be used after compilation 1 and 0 To express , These two numbers only need... In memory 1 position (bit) You can store , Bit is the smallest storage unit of a computer .
2、1 Bytes
The reason is that although compiled 1 and 0 Just occupy 1 Bitspace , But the smallest unit of data processed by a computer is 1 Bytes ,1 Bytes are equal to 8 position , The actual storage space is : use 1 The lowest byte of storage , other 7 Bit use 0 fill , If the value is true The stored binary is :0000 0001, If it is false The stored binary is :0000 0000.
3、4 Bytes
The reason source is 《Java Virtual machine specification 》 Description in a Book :“ Although the definition of boolean This data type , But only very limited support for it . stay Java There is nothing in the virtual machine for boolean Value specific bytecode instructions ,Java Language expressions operate on boolean value , Use... After compilation Java In the virtual machine int Data type instead of , and boolean The array will be encoded as Java The virtual machine byte Array , Every element boolean Elements take up 8 position ”. So we can get boolean The type occupied by the single use is 4 Bytes , In the array is again 1 Bytes .
summary
stay 《Java Virtual machine specification 》 A single boolean Occupy 4 Bytes , and boolean Array 1 Definition of bytes , Specifically still more See whether the virtual machine implementation is in accordance with the specification , therefore 1 Bytes 、4 Every byte is possible .
边栏推荐
- Excuse me, how to deal with repeated consumption of MySQL data
- 【饭谈】测试平台为什么有组件化?模块化?很多看不到的地方设计的很好是种浪费么?
- QML module not found
- 卸载npm和安装npm_使用`npm uninstall`卸载npm软件包「建议收藏」
- 2022最新软件测试八股文,能不能拿心仪Offer就看你背得怎样了
- Flex layout
- Sofa weekly | open source person - Niu Xuewei, QA this week, contributor this week
- 【饭谈】细说:下克上,向上管理,向上画饼。
- How is it most convenient to open an account for stock speculation? Is it safe for online account managers to open an account
- El expression improves JSP
猜你喜欢

MySQL --- 子查询 - 列子查询(多行子查询)

虚拟内存与磁盘

Application of breakthrough thinking in testing work
![[dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality](/img/11/42c4674d23ee93850fb3d2de0d0932.png)
[dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality "eye edge" during interview)

还不懂mock测试?一篇文章带你熟悉mock

『Skywalking』. Net core fast access distributed link tracking platform

SQL中in的用法 DQL 查询

kubernetes之VictoriaMetrics单节点

Wet- a good choice for people with English difficulties - console translation

核电站在席卷欧洲的热浪中努力保持安全工作
随机推荐
开户就可以购买收益在百分之六以上的理财产品了吗
信息安全建设原则指导
Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (judgment question) June 2022
Redis为何选择单线程?
Why does redisv6.0 introduce multithreading?
LeetCode_ 93_ Restore IP address
Animation curves are used every day. Can you make one by yourself? After reading this article, you will!
2day
The file cannot be saved (what if the folder is damaged and cannot be read)
jsp九大内置对象
如何将一个域名解析到多个IP地址?
五种分配方式是否会产生内部碎片、外部碎片
数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir
SQL中in的用法 DQL 查询
Can I buy financial products with a revenue of more than 6% after opening an account
El expression improves JSP
[dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality "eye edge" during interview)
Jmeter---设置代理录制请求
c sqlite ... ...
In Oracle 19C version, logminer package continuous_ The outdated function of mine leads to CDC failure