Java中布尔变量的大小是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/383551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the size of a boolean variable in Java?
提问by DonX
Can any one tell the bit size of booleanin Java?
任何人都可以告诉Java中布尔值的位大小吗?
采纳答案by Warrior
It's virtual machine dependent.
它依赖于虚拟机。
回答by William Brendel
The actual information represented by a boolean value in Java is one bit: 1 for true, 0 for false. However, the actual size of a boolean variable in memory is not precisely defined by the Java specification. See Primitive Data Types in Java.
Java 中布尔值表示的实际信息是一位:1 表示真,0 表示假。但是,Java 规范并未精确定义内存中布尔变量的实际大小。请参阅Java 中的原始数据类型。
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.
布尔数据类型只有两个可能的值:true 和 false。将此数据类型用于跟踪真/假条件的简单标志。这种数据类型代表一位信息,但它的“大小”并不是精确定义的。
回答by rics
回答by Jon Skeet
It depends on the virtual machine, but it's easy to adapt the code from a similar question asking about bytes in Java:
这取决于虚拟机,但很容易改编来自类似问题的代码,该问题询问 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 i=0; i < SIZE; i++)
{
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 i=0; i < SIZE; i++)
{
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 i=0; i < SIZE; i++)
{
total += (first[i].a0 ? 1 : 0) + second[i].a0;
}
System.out.println(total);
}
private static long getMemory()
{
Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
}
}
To reiterate, this is VM-dependent, but on my Windows laptop running Sun's JDK build 1.6.0_11 I got the following results:
重申一下,这取决于 VM,但在运行 Sun JDK build 1.6.0_11 的 Windows 笔记本电脑上,我得到了以下结果:
Size for LotsOfBooleans: 87978576
Average size: 87.978576
Size for LotsOfInts: 328000000
Average size: 328.0
That suggests that booleans can basically be packed into a byte each by Sun's JVM.
这表明布尔值基本上可以被 Sun 的 JVM 打包成一个字节。
回答by Lawrence Dol
It's undefined; doing things like Jon Skeet suggested will get you an approximation on a given platform, but the way to know precisely for a specific platform is to use a profiler.
它是未定义的;像 Jon Skeet 建议的那样做可以让您在给定平台上获得近似值,但是准确了解特定平台的方法是使用分析器。
回答by Matthew Schinckel
On a side note...
在旁注...
If you are thinking about using an array of Boolean objects, don't. Use a BitSet instead - it has some performance optimisations (and some nice extra methods, allowing you to get the next set/unset bit).
如果您正在考虑使用布尔对象数组,请不要。改用 BitSet - 它有一些性能优化(以及一些不错的额外方法,允许您获得下一个设置/取消设置位)。
回答by JavaNewbie_M107
回答by Deepak Odedara
Size of the boolean in java is virtual machine dependent. but Any Java object is aligned to an 8 bytes granularity. A Boolean has 8 bytes of header, plus 1 byte of payload, for a total of 9 bytes of information. The JVM then rounds it up to the next multiple of 8. so the one instance of java.lang.Boolean takes up 16 bytes of memory.
java中布尔值的大小取决于虚拟机。但是任何 Java 对象都对齐到 8 字节的粒度。一个布尔值有 8 个字节的头部,加上 1 个字节的有效载荷,总共有 9 个字节的信息。JVM 然后将其四舍五入到下一个 8 的倍数。因此 java.lang.Boolean 的一个实例占用 16 字节的内存。