小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

深入Java關(guān)鍵字null

 燮羽 2010-12-05
一、null是代表不確定的對象
 
Java中,null是一個關(guān)鍵字,用來標(biāo)識一個不確定的對象。因此可以將null賦給引用類型變量,但不可以將null賦給基本類型變量。
 
比如:int a = null;是錯誤的。Ojbect o = null是正確的。
 
Java中,變量的適用都遵循一個原則,先定義,并且初始化后,才可以使用。我們不能int a后,不給a指定值,就去打印a的值。這條對對于引用類型變量也是適用的。
 
有時候,我們定義一個引用類型變量,在剛開始的時候,無法給出一個確定的值,但是不指定值,程序可能會在try語句塊中初始化值。這時候,我們下面使用變量的時候就會報錯。這時候,可以先給變量指定一個null值,問題就解決了。例如:
 
        Connection conn = null;
        try {
            conn = DriverManager.getConnection("url", "user", "password");
        } catch (SQLException e) {
            e.printStackTrace();
        }
 
        String catalog = conn.getCatalog();
 
如果剛開始的時候不指定conn = null,則最后一句就會報錯。
 
二、null本身不是對象,也不是Objcet的實例
 
null本身雖然能代表一個不確定的對象,但就null本身來說,它不是對象,也不知道什么類型,也不是java.lang.Object的實例。
可以做一個簡單的例子:
 
        //null是對象嗎? 屬于Object類型嗎?
        if (null instanceof java.lang.Object) {
            System.out.println("null屬于java.lang.Object類型");
        } else {
            System.out.println("null不屬于java.lang.Object類型");
        }
 
結(jié)果會輸出:null不屬于java.lang.Object類型
 
三、Java默認(rèn)給變量賦值
 
在定義變量的時候,如果定義后沒有給變量賦值,則Java在運行時會自動給變量賦值。賦值原則是整數(shù)類型int、byte、short、long的自動賦值為0,帶小數(shù)點的float、double自動賦值為0.0,boolean的自動賦值為false,其他各供引用類型變量自動賦值為null。
這個具體可以通過調(diào)試來看。
 
四、容器類型與null
 
List:允許重復(fù)元素,可以加入任意多個null。
Set:不允許重復(fù)元素,最多可以加入一個null。
Map:Map的key最多可以加入一個null,value字段沒有限制。
數(shù)組:基本類型數(shù)組,定義后,如果不給定初始值,則java運行時會自動給定值。引用類型數(shù)組,不給定初始值,則所有的元素值為null。
 
五、null的其他作用
 
1、判斷一個引用類型數(shù)據(jù)是否null。 用==來判斷。
2、釋放內(nèi)存,讓一個非null的引用類型變量指向null。這樣這個對象就不再被任何對象應(yīng)用了。等待JVM垃圾回收機制去回收。

java語言規(guī)范中是這么規(guī)定的: 

A literal is the source code representation of a value of a primitive type , the String type , or the null type : 

 Literal:     IntegerLiteral     FloatingPointLiteral     BooleanLiteral     CharacterLiteral     StringLiteral     NullLiteral 

While true and false might appear to be keywords, they are technically Boolean literals (§3.10.3). Similarly, while null might appear to be a keyword, 
it is technically the null literal (§3.10.7). 
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?!會被打印出來,而后邊一句會打印出null,我們知道println一般是調(diào)用對象的toString來進行打印,但是null明顯不是對象,作為一個不確定引用它只是把自己的literal給打印了出來,因此我猜測null是預(yù)先存儲在內(nèi)存中的一個引用,它是有值的,可能根據(jù)不同的jvm有不同的實現(xiàn),但是jvm中會一致地將這個值打印為null。
 
另外按照我的猜測,null應(yīng)該是棧里邊的一個引用,null type是可以轉(zhuǎn)換為所有其他type的引用,所以我也猜測null type應(yīng)該是所有其他引用類型的子類型。
靜態(tài)方法(也許需要是public的)應(yīng)該是由類名來調(diào)用,但是也可以使用 類名(null).靜態(tài)方法名 的方式調(diào)用,具體細(xì)節(jié)沒太弄懂。
 
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).
 
再引用另外一篇文章,雖然根本上就錯了,但還是有些內(nèi)容的:
 
作為關(guān)鍵字true,false,null都分別代表不同的含義。
 
"
"位"是內(nèi)存中作為存儲數(shù)據(jù)的基本單位,而我們又通常說,一個字節(jié)是8位,也就是 1byte = 8bit。
 
因為內(nèi)存中,"位"是使用0和1來表示的,所以作為關(guān)鍵字,true的值在內(nèi)存中就表示1,false在內(nèi)存中就是表示0。
 
但是這里不要和整數(shù)(int)中的0和1相比,他們占用的內(nèi)存空間是不一樣的。一個int型的變量,占用的內(nèi)存空間是4個字節(jié),也就是4 * 8 = 32位,與true和false占用的內(nèi)存空間是不同的。
 
而作為關(guān)鍵字null,解釋起來就更是麻煩了。
 
當(dāng)一個對象被聲明時(Object o;),這個對象會以一個整數(shù)的字節(jié)數(shù),只在內(nèi)存堆棧(stack)中開辟一個內(nèi)存指針。例如,我們使用Object o = new Object();實例化一個對象,那么在內(nèi)存中的運行則是:在內(nèi)存堆棧(stack)中開辟一個4個字節(jié)的指針,然后在內(nèi)存堆區(qū)(heap)開辟這個對象所要存儲的數(shù)據(jù)并初始化,然后在將之前在stack中的內(nèi)存指針中賦上在heap中開辟的空間的首地址。
 
而如果Object o;沒有進行實例化,則不可能使用o的引用。這時為了方便使用,則出現(xiàn)了null關(guān)鍵字。null關(guān)鍵字的意義也就是一個用來初始化對象的空引用。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多