The null type has one value, the null reference, represented by the literal null,which is formed from ASCII characters. A null literal is always of the null type.
以下是我的一些想法:
package com.zac.expe1;
public class NullInDepth {
public static void helloThere(){
System.out.println("Hello from null?!");
}
public static void main(String[] args) {
//((NullInDepth)null).helloThere();
NullInDepth nullIn = null;
nullIn.helloThere();
Object nullStr = null;
NullInDepth nullOut = (NullInDepth) nullStr;
System.out.println(nullIn);
}
}
這里Hello from null?!會(huì )被打印出來(lái),而后邊一句會(huì )打印出null,我們知道println一般是調用對象的toString來(lái)進(jìn)行打印,但是null明顯不是對象,作為一個(gè)不確定引用它只是把自己的literal給打印了出來(lái),因此我猜測null是預先存儲在內存中的一個(gè)引用,它是有值的,可能根據不同的jvm有不同的實(shí)現,但是jvm中會(huì )一致地將這個(gè)值打印為null。
另外按照我的猜測,null應該是棧里邊的一個(gè)引用,null type是可以轉換為所有其他type的引用,所以我也猜測null type應該是所有其他引用類(lèi)型的子類(lèi)型。
靜態(tài)方法(也許需要是public的)應該是由類(lèi)名來(lái)調用,但是也可以使用 類(lèi)名(null).靜態(tài)方法名 的方式調用,具體細節沒(méi)太弄懂。
Since Java and C# run on virtual machines, it does not matter what is used physically to represent null, and it is not necessarily the same across implementations.
What matters is the behaviour of null, as defined in the language specification (see Dan's and MRFerocius' answers for details). Basically, it is a special value that variables of reference type can hold, and which cannot be dereferenced.
BTW, as a reference point, the Java serialization spec use a single byte value 0x70 to represent a null reference.
"null" has the interesting property that "instanceof" used with null always returns false.
When putting a cast before a literal null, remember this is only a syntax to help the compiler choose between overloaded methods. The cast doesn't actually "do anything" to the null reference. Casting doesn't do anything anyway. If it survives in bytecode at all, it only performs a check. Static types only really matter to the compiler (blah blah reflection blah blah bytecode contains some static types blah blah).
再引用另外一篇文章,雖然根本上就錯了,但還是有些內容的:
作為關(guān)鍵字true,false,null都分別代表不同的含義。
"
"位"是內存中作為存儲數據的基本單位,而我們又通常說(shuō),一個(gè)字節是8位,也就是 1byte = 8bit。
因為內存中,"位"是使用0和1來(lái)表示的,所以作為關(guān)鍵字,true的值在內存中就表示1,false在內存中就是表示0。
但是這里不要和整數(int)中的0和1相比,他們占用的內存空間是不一樣的。一個(gè)int型的變量,占用的內存空間是4個(gè)字節,也就是4 * 8 = 32位,與true和false占用的內存空間是不同的。
而作為關(guān)鍵字null,解釋起來(lái)就更是麻煩了。
當一個(gè)對象被聲明時(shí)(Object o;),這個(gè)對象會(huì )以一個(gè)整數的字節數,只在內存堆棧(stack)中開(kāi)辟一個(gè)內存指針。例如,我們使用Object o = new Object();實(shí)例化一個(gè)對象,那么在內存中的運行則是:在內存堆棧(stack)中開(kāi)辟一個(gè)4個(gè)字節的指針,然后在內存堆區(heap)開(kāi)辟這個(gè)對象所要存儲的數據并初始化,然后在將之前在stack中的內存指針中賦上在heap中開(kāi)辟的空間的首地址。
而如果Object o;沒(méi)有進(jìn)行實(shí)例化,則不可能使用o的引用。這時(shí)為了方便使用,則出現了null關(guān)鍵字。null關(guān)鍵字的意義也就是一個(gè)用來(lái)初始化對象的空引用。