爪哇 - 布尔基本类型 - 大小 [英] Java - boolean primitive type - size

查看:165
本文介绍了爪哇 - 布尔基本类型 - 大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助 Java虚拟机规范说,有布尔的原始类型有限的支持。

The Java Virtual Machine Specification says that there is limited support for boolean primitive types.

有没有Java虚拟机指令只专注于对​​布尔值操作。相反,在Java编程语言的前pressions上布尔值操作被编译使用Java虚拟机int数据类型的值。

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.

上面的暗示(虽然我可能有misinter preTED吧),关于布尔操作时,int数据类型使用,但是这是一个32位的内存结构。鉴于一个布尔值只能重新presents 1位的信息:

The above implies (although I may have misinterpreted it) that the int data type is used when operating on booleans, but this is a 32 bit memory construct. Given that a boolean only represents 1 bit of information:


  • 为什么一个字节,或短,类型不作为代理的布尔而不是诠释?

  • 对于任何给定的JVM什么是找出到底有多少内存用于存储布尔类型的最可靠的方法是什么?

推荐答案

简短的回答:是的,布尔值被操纵32位的实体,但布尔的阵列使用每个单元1个字节

Short answer: yes, boolean values are manipulated as 32-bit entities, but arrays of booleans use 1 byte per element.

更长的答案:JVM使用32位堆栈单元,用来存放局部变量,方法参数,和前pression值。基元是小于1的细胞会被填充了,基本单元大于32位(长双)取2细胞。这种技术减少了运算codeS的数量,但确实有一些特殊的副作用(如需要,以掩盖字节)。

Longer answer: the JVM uses a 32-bit stack cell, used to hold local variables, method arguments, and expression values. Primitives that are smaller than 1 cell are padded out, primitives larger than 32 bits (long and double) take 2 cells. This technique minimizes the number of opcodes, but does have some peculiar side-effects (such as the need to mask bytes).

存储在阵列基元可以使用小于32位,并且有不同的运算codeS加载和从数组存储的原始值。布尔和字节​​值都使用baload和bastore运codeS,这意味着布尔数组采取每个单元1个字节。

Primitives stored in arrays may use less than 32 bits, and there are different opcodes to load and store primitive values from an array. Boolean and byte values both use the baload and bastore opcodes, which implies that boolean arrays take 1 byte per element.

至于在内存中的对象布局得好,这就是私有实现所涵盖的规则的,它可以是1位,1字节,或作为另一海报所指出的,对齐一64位双字边界。最有可能的,它采用底层硬件(32位或64位)的基本字的大小。

As far as in-memory object layout goes, this is covered under the "private implementation" rules, it can be 1 bit, 1 byte, or as another poster noted, aligned to a 64-bit double-word boundary. Most likely, it takes the basic word size of the underlying hardware (32 or 64 bits).


至于减少的空间布尔使用量:它真的不是对于大多数应用程序的问题。堆栈帧(保持局部变量和方法参数)不是非常大,并且在大方案中的对象的离散布尔不是大任。如果你有很多,有很多布尔对象,那么你可以使用通过你的getter和setter管理位字段。然而,你支付的CPU时间的罚款是可能比在内存中的点球大。

As far as minimizing the amount of space that booleans use: it really isn't an issue for most applications. Stack frames (holding local variables and method arguments) aren't very large, and in the big scheme a discrete boolean in an object isn't that large either. If you have lots of objects with lots of booleans, then you can use bit-fields that are managed via your getters and setters. However, you'll pay a penalty in CPU time that is probably bigger than the penalty in memory.

这篇关于爪哇 - 布尔基本类型 - 大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆