2014-07-01 49 views
-1
public class Concept {  
    int num;  
    public static void main(String args[])  
    {  
     Concept obj1=new Concept();  
     Concept obj2=obj1;  
     obj1.num=100;  
     obj2.num=200;  
     System.out.println(obj1.num);  
     System.out.println(obj2.num);  
     //obj1.num=null;  

     obj2.num=500;  

     System.out.println(obj1.num);  
     System.out.println(obj2.num);  
    } 

}  

這裏,這是一個簡單的java程序。需要您的幫助瞭解java編程概念

輸出是

200 
200 
500 
500 

如何進行內存分配工作嗎?我們如何將null設置爲Obj1.num = null?

+1

你能更具體地說明你想要什麼嗎? –

回答

6

你一次問兩個問題。


Java使用引用,從而通過設置obj2 = obj1,這些值不復制,兩個引用指代相同的對象。設置numobj1的值與通過obj2進行設置的值相同。

在內存方面,它看起來像:

 +-----------------+ 
obj2 -> | Concept  | <- obj1 
     +-----+-----+-----+ 
     | num | int | 200 | 
     +-----+-----+-----+ 

,如果您然而使用下面的代碼,兩個對象將被修建:

Concept obj1 = new Concept(); 
obj1.num = 200; 
Concept obj2 = new Concept(); 
obj2.num = 300; 

那麼內存會是什麼樣子:

 +-----------------+   +-----------------+ 
obj1 -> | Concept  | obj2 -> | Concept  | 
     +-----+-----+-----+   +-----+-----+-----+ 
     | num | int | 200 |   | num | int | 300 | 
     +-----+-----+-----+   +-----+-----+-----+ 

因此從obj1修改num會設置一個不同的部分內存比從obj2


您不能分配nullint:這是一個基本類型。如果您想要一個具有null值的整數,請嘗試Integer

7

obj2具有obj1相同的參考,所以修改任何這些狀態將在這兩個變量被反射,因爲它們是指向相同的附圖。

這裏有一個圖片來解釋對象引用(使用Person類在這種情況下解釋)的行爲:

enter image description here

來源:https://stackoverflow.com/a/7034719/1065197

請注意,你不能抵消一個int也不Java中的任何原始值,因此obj1.num = null無效。但是,如果您聲明numInteger,則可以將其值設置爲null,因爲Integer是原型int包裝類

2

您不能,因爲您聲明numint,這是原始類型

你能做什麼? 您可以使用它wrapper classInteger

Integer num; 
... 
num = null; // valid since num is now an object 
3

Java不是C++,

Concept obj1=new Concept();  
Concept obj2=obj1; // obj2 is a reference to obj1. 

我想你想,

Concept obj1=new Concept();  
Concept obj2=new Concept(); 

而且,你不能設置一個基本類型(如int)爲空。你可以一個Integer包裝設置爲null,

Concept {  
// int num; 
Integer num = null; 
2
public class Concept {  
    int num;  
    public static void main(String args[])  
    {  
     Concept obj1=new Concept(); // Only one Concept object created. The reference obj1 is pointing to it. 
     Concept obj2=obj1; // now 2 references obj1 and obj2 are pointing to the same object. 
     obj1.num=100; // since both references are pointing to the same object, the changes they make will be on the `same` object 
     obj2.num=200;  
     System.out.println(obj1.num);  
     System.out.println(obj2.num);  
     //obj1.num=null;  

     obj2.num=500;  

     System.out.println(obj1.num);  
     System.out.println(obj2.num);  
    } 

}  
1
Obj1.num=null; 

num不能設置爲空,因爲num是原始int而不是對象。在Java中,只能將對象引用設置爲null。

至於內存處理。我在下面註釋了你的代碼,以解釋發生了什麼。

public class Concept {  
    int num;  
    public static void main(String args[])  
    {  
     Concept obj1=new Concept();  // A new object is created. Ref stored in obj1 
     Concept obj2=obj1;    // The ref from obj1 is copied to obj2 
     obj1.num=100;     // obj1 and obj2 now hold the same ref to the same object 
     obj2.num=200;     // overwrites previous assignment; the java compiler is likely to strip out the previous line as it is redundant 
     System.out.println(obj1.num);  
     System.out.println(obj2.num);  
     //obj1.num=null;  

     obj2.num=500;     // as before 

     System.out.println(obj1.num);  
     System.out.println(obj2.num);  
    } 

}  

值得注意的是,與其他語言不同,Java總是在堆上創建其對象。一個由其管理的內存區域,它爲我們收集垃圾。它不會在堆棧中創建對象,因此Object類型的字段和變量始終是對象的引用。

+0

+1單行註釋* – TheLostMind