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.
“its "size" isn't something that's precisely defined. ” 这句话学问很大,在介绍boolean的时候,首先提到是“This data type represents one bit of information”,也就是说boolean使用了一个比特的信息,通常我们会猜测,编译器会给一个boolean变量 一个字节的内存,但只使用最后一个比特位,但如果是这样的话,那位什么sun在文档里不直接将boolean变量 定义成一个字节,而是给出上面的一段含义暧昧的话呢?简单点讲,编译器对boolean变量进行了优化,在一个boolean变量情况下,编译器会给此变量安排一个字节的 内存,但在多个boolean的情况下时,编译器会将多个变量安排在一个字节里
int a = 0; bool b = false; int c = 3; 这段C++代码,你说这几个变量占用几个字节?难道是9个字节嘛?其实会把它优化到12个字节的空间上,否则对c的操作是不利的。 我们今天的CPU处理4BYTEs的内容更加得心应手了。而且C实现JVM里面的变量的时候, 会用到union这个关键字。union { int a; bool b; } 你说整个结构犯得着在Boolean上节省空间嘛? 还有就是,数组和单个的变量很有可能不是一致对待的。如果是数组的话,也许用不到一个字节的空间存储更核算,但是依然会触及到存储效率的问题。 http://www.rdbbs.net/ShowPost.aspx?Language=zh-cn&ForumID=42&TopicID=2206&PostID=10516&SkinType=0&Page=1 看看这个3楼的,就说明这个问题其实是很复杂的,不会是一个简单的答案的。
vc++是四个字节,不是一个bit Boolean variable (should be TRUE or FALSE). This type is declared in WinDef.h as follows: typedef int BOOL;
答:我的结论是: 1)boolean a=true;//这个a在JVM中占4个字节即:32位。 2)boolean[] b = new boolean[10];//数组时,每一个boolean在JVM中占一个字节。 理由: 1)JAVA规范中没有定义boolean类型的大小。 2)但是:在JVM规范第2版中讲得十分清楚。我上边的结论就是从它当中取出来的。 根据:(JVM规范第2版 3.3.4节) 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. Where Java programming language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding. 而:Java virtual machine type int, whose values are 32-bit signed two's-complement integers。 Arrays of type boolean are accessed and modified using the byte array instructions In Sun's JDK releases 1.0 and 1.1, and the Java 2 SDK, Standard Edition, v1.2, boolean arrays in the Java programming language are encoded as Java virtual machine byte arrays, using 8 bits per boolean element.
答:你应先认真读一读《The JavaTM Virtual Machine Specification(Second Edition)》中“3.3.4 The boolean Type”这一节,再回答是不是“正解”。
内存中占多大并不重要,反正它只有两个值。 但在大多数JVM实现中,应该是一个字节。
boolean: The boolean data type has only two possiblevalues: true and false.
package com.base; public class Sizeof { public static void main (String [] args) throws Exception { // Warm up all classes/methods we will use runGC (); usedMemory (); // Array to keep strong references to allocated objects final int count = 100000;
Object [] objects = new Object [count];
long heap1 = 0;
// Allocate count+1 objects, discard the first one for (int i = -1; i < count; ++ i) { Object object = null; // Instantiate your data here and assign it to object // object = new Object (); // object = new Integer (i); object = new Boolean(true); //object = new Long (i); //object = new String (); //object = new byte [128][1] if (i >= 0) objects [i] = object; else { object = null; // Discard the warm up object runGC (); heap1 = usedMemory (); // Take a before heap snapshot } } runGC (); long heap2 = usedMemory (); // Take an after heap snapshot:
final int size = Math.round (((float)(heap2 - heap1))/count); System.out.println ("'before' heap: " + heap1 + ", 'after' heap: " + heap2); System.out.println ("heap delta: " + (heap2 - heap1) + ", {" + objects [0].getClass () + "} size = " + size + " bytes"); for (int i = 0; i < count; ++ i) objects [i] = null; objects = null; } private static void runGC () throws Exception { // It helps to call Runtime.gc() // using several method calls: for (int r = 0; r < 4; ++ r) _runGC (); } private static void _runGC () throws Exception { long usedMem1 = usedMemory (), usedMem2 = Long.MAX_VALUE;
for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++ i) { s_runtime.runFinalization (); s_runtime.gc (); Thread.currentThread().yield (); usedMem2 = usedMem1; usedMem1 = usedMemory (); } } private static long usedMemory () { return s_runtime.totalMemory ()- s_runtime.freeMemory (); } private static final Runtime s_runtime = Runtime.getRuntime (); } // End of class
boolean 1 位 - - Boolean
char 16 位 Unicode 0 Unicode 2 的16 次方-1 Character
byte 8 位 -128 +127 Byte(注释①)
short 16 位 -2 的15 次方 +2 的15 次方-1 Short(注释①)
int 32 位 -2 的31 次方 +2 的31 次方-1 Integer
long 64 位 -2 的63 次方 +2 的63 次方-1 Long
float 32 位 IEEE754 IEEE754 Float
double 64 位 IEEE754 IEEE754 Double^_^把别人的答案复制来
下面是sun公司的官方文档 我好像看到说boolean 占1 bit 不是 1 byte
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
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.
boolean占1byte.
(1)在虚拟机里boolean在编译成字节码时会用int或byte来表示。false用整数0表示,true用非零整数表示。涉及boolean的操作是用int进行的。boolean数据是当成byte数组进行访问的。
(2)JAVA虚拟机中,基本的数据单元是字(word)大小由虚拟机的设计而定。一般为32位。虚拟机的局部变量和操作数栈都是按照字来字义的。
(以上摘自《深入JAVA虚拟机》(第二版))因此我觉得和虚拟机有关。一般为32位。以上说法,和大家讨论
这句话学问很大,在介绍boolean的时候,首先提到是“This data type represents one bit of information”,也就是说boolean使用了一个比特的信息,通常我们会猜测,编译器会给一个boolean变量
一个字节的内存,但只使用最后一个比特位,但如果是这样的话,那位什么sun在文档里不直接将boolean变量
定义成一个字节,而是给出上面的一段含义暧昧的话呢?简单点讲,编译器对boolean变量进行了优化,在一个boolean变量情况下,编译器会给此变量安排一个字节的
内存,但在多个boolean的情况下时,编译器会将多个变量安排在一个字节里
(1)在虚拟机里boolean在编译成字节码时会用int或byte来表示。false用整数0表示,true用非零整数表示。涉及boolean的操作是用int进行的。boolean数据是当成byte数组进行访问的。
(2)JAVA虚拟机中,基本的数据单元是字(word)大小由虚拟机的设计而定。一般为32位。虚拟机的局部变量和操作数栈都是按照字来字义的。
(以上摘自《深入JAVA虚拟机》(第二版))因此我觉得和虚拟机有关。一般为32位。以上说法,和大家讨论
========================================================================================================================================不用讨论了,就这个了。
补充说明的是,通常所说的字是WORD,但是是16位的,DWORD才是32位,叫双字。但是你这里给出的第二条,似乎不是相关的。
第一条里面说的,所谓操作是指代传参等,但是并不表示存储。function(long bValue)
{
}function(true);
比如这里,一个单字节的量,传递给4字节的整形量。我印象中,C#某个原始类型,好像就是bool。在数组中的存储,和单个的变量的存储也是有差别的,所以JVM可能也有这样的优化存储。书不在手头。
所以可能是4BYTE存储,是因为今天的32位系统对于4字节的数据的存取速度快,所以有所谓内存对齐这一说。
所以,1BYTE/ 1/2BYTE, 4BYTE都有可能,不太可能的是1BIT和2BYTE。PS:下次说几楼,不要说上面的。
记得以前有个“有几个string参象”的帖子 跟帖的有一百多人 也没讨论出个结果来。
还有就是版主就要发挥版主的作用
(等待被抽。)
byte b = 1;这个占用多大的内存呢?我并不认为会占用32bit = 4byte
32 位是运行代码的长度
比如以前是16位机器,使用 AX 寄存器,后来32位机器,变成了EAX但并不代表内存就是这样分配的,因为EAX也是可以存取一个8位的地址空间的。mov eax, byte prt [300000];这样是可以的。
所以,我依然认为,boolean 使用了一个bit,但占用了一个byte,而不是4个。
byte 8bit
short 16bit
int 32bit
long 64bit
float 32bit
double 64bit
大家~
使用了1BIT是不懂汇编的人都知道的。
我们就是在讨论占用的问题,我也只是说可能占用4BYTEs。
int a = 0;
bool b = false;
int c = 3;
这段C++代码,你说这几个变量占用几个字节?难道是9个字节嘛?其实会把它优化到12个字节的空间上,否则对c的操作是不利的。
我们今天的CPU处理4BYTEs的内容更加得心应手了。而且C实现JVM里面的变量的时候,
会用到union这个关键字。union {
int a;
bool b;
}
你说整个结构犯得着在Boolean上节省空间嘛?
还有就是,数组和单个的变量很有可能不是一致对待的。如果是数组的话,也许用不到一个字节的空间存储更核算,但是依然会触及到存储效率的问题。
http://www.rdbbs.net/ShowPost.aspx?Language=zh-cn&ForumID=42&TopicID=2206&PostID=10516&SkinType=0&Page=1
看看这个3楼的,就说明这个问题其实是很复杂的,不会是一个简单的答案的。
得闲切磋一下...
Boolean variable (should be TRUE or FALSE).
This type is declared in WinDef.h as follows:
typedef int BOOL;
1)boolean a=true;//这个a在JVM中占4个字节即:32位。
2)boolean[] b = new boolean[10];//数组时,每一个boolean在JVM中占一个字节。
理由:
1)JAVA规范中没有定义boolean类型的大小。
2)但是:在JVM规范第2版中讲得十分清楚。我上边的结论就是从它当中取出来的。
根据:(JVM规范第2版 3.3.4节)
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.
Where Java programming language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding. 而:Java virtual machine type int, whose values are 32-bit signed two's-complement integers。
Arrays of type boolean are accessed and modified using the byte array instructions
In Sun's JDK releases 1.0 and 1.1, and the Java 2 SDK, Standard Edition, v1.2, boolean arrays in the Java programming language are encoded as Java virtual machine byte arrays, using 8 bits per boolean element.
但在大多数JVM实现中,应该是一个字节。
自己建立一个大的boolean数组,然后观察内存值可以得到结论.
public class Sizeof
{
public static void main (String [] args) throws Exception
{
// Warm up all classes/methods we will use
runGC ();
usedMemory ();
// Array to keep strong references to allocated objects
final int count = 100000;
Object [] objects = new Object [count];
long heap1 = 0;
// Allocate count+1 objects, discard the first one
for (int i = -1; i < count; ++ i)
{
Object object = null;
// Instantiate your data here and assign it to object
// object = new Object ();
// object = new Integer (i);
object = new Boolean(true);
//object = new Long (i);
//object = new String ();
//object = new byte [128][1]
if (i >= 0)
objects [i] = object;
else
{
object = null; // Discard the warm up object
runGC ();
heap1 = usedMemory (); // Take a before heap snapshot
}
}
runGC ();
long heap2 = usedMemory (); // Take an after heap snapshot:
final int size = Math.round (((float)(heap2 - heap1))/count);
System.out.println ("'before' heap: " + heap1 +
", 'after' heap: " + heap2);
System.out.println ("heap delta: " + (heap2 - heap1) +
", {" + objects [0].getClass () + "} size = " + size + " bytes");
for (int i = 0; i < count; ++ i) objects [i] = null;
objects = null;
}
private static void runGC () throws Exception
{
// It helps to call Runtime.gc()
// using several method calls:
for (int r = 0; r < 4; ++ r) _runGC ();
}
private static void _runGC () throws Exception
{
long usedMem1 = usedMemory (), usedMem2 = Long.MAX_VALUE;
for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++ i)
{
s_runtime.runFinalization ();
s_runtime.gc ();
Thread.currentThread().yield ();
usedMem2 = usedMem1;
usedMem1 = usedMemory ();
}
}
private static long usedMemory ()
{
return s_runtime.totalMemory ()- s_runtime.freeMemory ();
}
private static final Runtime s_runtime = Runtime.getRuntime ();
} // End of class
heap delta: 1599664, {class java.lang.Boolean} size = 16 bytes
所以true在内存中表示为 0x00000001
所以 还是一个 BYTE 即一个字节
说明一个boolean变量实际占用1Byte