日期:2014-05-20  浏览次数:20675 次

java中null,true,false各占几个字节,在底层分别如何用二进制形式表示
请问各位大神:java中null值本身占内存吗,在c语言里null是0,那么在java里null是多少呢,在底层如何用二进制来表示null呢,同样的还有false和true,如何用二进制形式表示出来。

------解决方案--------------------
这个应该是看虚拟机怎样实现的了吧,可能不同的虚拟机的具体实现方式并不相同
------解决方案--------------------
Java规范并不限制JVM以什么方式实现boolean的存储,null的存储,所以完全看JVM的实现机制。

Sun的JVM中boolean大致是:
1、如果是单个变量,则以int来实际存储的,这样速度快;
2、如果是数组,则以byte[]来实际存储,在速度和空间上做了折中。

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 Java? Virtual Machine Specification
------解决方案--------------------
http://www.iteye.com/topic/1117824
------解决方案--------------------
就现在通用的jdk实现来说,boolean在方法栈当中,使用的是int,在类属性中似乎是用short。至于数组,我就不清楚了。就jvm而言,不存在boolean类型变量。