2014-06-15 42 views
1

在java中遇到new關鍵字時,會創建一個對象並調用構造函數。數組實例化調用構造函數?

創建數組對象時調用哪個構造函數。

例如int [] a = new int [];

我們知道數組是對象。

回答

2

數組不使用構造函數進行初始化。如果你嘗試編譯int[] array = new int[10];你會得到類似下面的字節碼:

bipush 10 
newarray int 
astore_1 

在另一方面,對於Person p = new Person();的字節碼指令將類似於以下(注意調用newinit表示在調用構造函數):

new test/Person 
dup 
invokespecial test/Person/<init>()V 
astore_2 

因此陣列有自己的比創建類對象不同的JVM的創建方式。

相關問題