Can any one tell the bit size of boolean in Java?
谁能告诉我们Java中布尔值的大小?
10 个解决方案
#1
42
It's virtual machine dependent.
这是虚拟机相关的。
#2
184
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依赖的,但是在我的Windows笔记本上运行Sun的JDK 1.6.0_11,我得到了以下结果:
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打包成一个字节。
#3
31
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比特:1为true, 0为false。然而,在内存中一个布尔变量的实际大小并不是由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。使用此数据类型为跟踪真实/错误条件的简单标志。这个数据类型代表一个信息,但是它的“大小”并不是精确定义的。
#4
20
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——它有一些性能优化(以及一些不错的额外方法,允许您获得下一个set/unset比特)。
#5
6
I read that Java reserves one byte for a boolean
datatype, but it uses only one bit. However, the documentation says that "its "size" isn't something that's precisely defined". See here.
我读到Java为布尔数据类型保留了一个字节,但是它只使用了一点。然而,文档说明“它的大小并不是精确定义的”。在这里看到的。
#6
3
I feel very happy that I have got the answer just now. so ,now ,i want to share it with all of you.
我很高兴我刚才得到了答案。现在,我想和大家分享一下。
firstly ,let us read the Oracle document at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html,
首先,让我们读一下Oracle文档:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html,
then you will read this as bellow:
然后你会读到如下:
"boolean: 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."
布尔:布尔数据类型只有两个可能的值:真和假。使用此数据类型为跟踪真实/错误条件的简单标志。这个数据类型代表了一点信息,但是它的“大小”并不是精确定义的。
hence,we can knew that the boolean's size is not defined,it maybe 4 bytes likes int, or it maybe 1 bytes likes byte, but ,what i can make sure is that it can not be 1 bit. i think the reason is obviously.
因此,我们可以知道布尔的大小没有定义,它可能有4个字节喜欢整数,或者1个字节喜欢字节,但是,我能确定的是它不能是1比特。我认为原因很明显。
wish this will help you.
希望这对你有帮助。
#8
2
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的一个实例。布尔占用16字节的内存。
#9
0
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这样做会让你在一个给定的平台上得到一个近似,但是准确地知道一个特定平台的方法是使用一个分析器。
#10
0
It defined by different java machine .
它由不同的java机器定义。
#1
42
It's virtual machine dependent.
这是虚拟机相关的。
#2
184
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依赖的,但是在我的Windows笔记本上运行Sun的JDK 1.6.0_11,我得到了以下结果:
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打包成一个字节。
#3
31
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比特:1为true, 0为false。然而,在内存中一个布尔变量的实际大小并不是由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。使用此数据类型为跟踪真实/错误条件的简单标志。这个数据类型代表一个信息,但是它的“大小”并不是精确定义的。
#4
20
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——它有一些性能优化(以及一些不错的额外方法,允许您获得下一个set/unset比特)。
#5
6
I read that Java reserves one byte for a boolean
datatype, but it uses only one bit. However, the documentation says that "its "size" isn't something that's precisely defined". See here.
我读到Java为布尔数据类型保留了一个字节,但是它只使用了一点。然而,文档说明“它的大小并不是精确定义的”。在这里看到的。
#6
3
I feel very happy that I have got the answer just now. so ,now ,i want to share it with all of you.
我很高兴我刚才得到了答案。现在,我想和大家分享一下。
firstly ,let us read the Oracle document at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html,
首先,让我们读一下Oracle文档:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html,
then you will read this as bellow:
然后你会读到如下:
"boolean: 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."
布尔:布尔数据类型只有两个可能的值:真和假。使用此数据类型为跟踪真实/错误条件的简单标志。这个数据类型代表了一点信息,但是它的“大小”并不是精确定义的。
hence,we can knew that the boolean's size is not defined,it maybe 4 bytes likes int, or it maybe 1 bytes likes byte, but ,what i can make sure is that it can not be 1 bit. i think the reason is obviously.
因此,我们可以知道布尔的大小没有定义,它可能有4个字节喜欢整数,或者1个字节喜欢字节,但是,我能确定的是它不能是1比特。我认为原因很明显。
wish this will help you.
希望这对你有帮助。
#7
#8
2
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的一个实例。布尔占用16字节的内存。
#9
0
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这样做会让你在一个给定的平台上得到一个近似,但是准确地知道一个特定平台的方法是使用一个分析器。
#10
0
It defined by different java machine .
它由不同的java机器定义。