2013-02-05 217 views
-5

當我們打印數組初始化的引用變量時會發生什麼?Java數組初始化

int[] it=new int[10]; 
sop(it); 

結果是什麼?

+0

當你嘗試過什麼事? –

+3

什麼是'sop()'? – amphibient

+0

數組也是一個對象。 –

回答

0

假設sopSystem.out.println,它將顯示由toString方法返回的字符串結果。在這種情況下,它將是類的名稱+「@」+哈希碼的十六進制。

0

喜歡的東西[[email protected]

這是你的新陣列的存儲地址

int[] it=new int[10]; 
    System.out.println(it); 
3
int[] it = new int[10]; 
System.out.println(it); 

it是一個對象,因此,您所呼叫的PrintStreamprintln(Object)System.out),其在電話toString()內部傳遞對象。陣列toString()類似於ObjecttoString()

getClass().getName() + "@" + Integer.toHexString(hashCode()); 

所以輸出會是這樣的:

[[email protected] 

[其中represnts陣列的深度,並且Iint756a7c99是作爲十六進制數從hashCode()返回的值。

閱讀Class.getName() JavaDoc


要打印一個數組,使用Arrays.toString(),像:

int[] it = new int[10]; 
System.out.println(Arrays.toString(it)); 

OUTPUT:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]