2010-05-20 60 views

回答

5

沒有,分配的原始值不創建任何對象。

什麼你可能被提及的是,原始值可以自動裝箱到相應的包裝,當他們在需要引用類型(又名「對象」)的上下文中使用:

int i = 13;  // this line does not create an object 
Integer i2 = i; // at this line 13 is auto-boxed into an Integer object 

char c = 'x'; // again: no object created: 
List<Character> l = new ArrayList<Character>(); 
l.add(c);  // c is auto-boxed into a Character object 

而且,我會盡力來形容聲明之間的區別和初始化:

int i;   // an int-variable is declared 
int j = 0;  // an int-variable is declared and initialized 
i = 1;   // an int-variable is assigned a value, this is *not* initialization 

一個變量被「申報」首次創建(當它即指定類型和名稱的變化iable)。它在聲明期間被賦值爲時被初始化。

0

編號基元不是java中的對象。

3

不,聲明和初始化一個基本變量不是創建一個對象。我們來看一個帶有兩個整數值的類 - 一個使用包裝類型,另一個不使用。

public class Foo 
{ 
    private int primitive = 10; 
    private Integer wrapper = new Integer(10); 
} 

primitive變量的只是數字10的wrapper變量的值是一個參考Integer對象又包含數字10因此的Foo一個實例將請保留primitive中的號碼以及wrapper中的參考號。

There are Java中所有基元類型的包裝類,但不會自動使用它們。

2

創建原語不是也爲它們創建包裝類。

至於你的原始問題:聲明/初始化一個原語將在堆棧上創建它,而聲明一個對象將分配一個變量來保存對一個對象的引用。初始化對象將在堆上分配它。