2010-06-22 42 views
2

我理解以下Java輸出。不理解Echo e2 = e1在Java中

public class EchoTestDrive { 
    public static void main(String[] args){ 
     Echo e1 = new Echo(); 
     Echo e2 = new Echo(); 
     int x = 0; 
     while (x<4){ 
      e1.hello(); 
      e1.count = e1.count + 1; 
      if (x==3){ 
       e2.count = e2.count +1; 
      } 
      if(x>0){ 
       e2.count = e2.count + e1.count; 
      } 
      x = x+1; 
      System.out.println("e1.count is " + e1.count); 
      System.out.println("e2.count is " + e2.count); 
     } 
     System.out.println(e2.count); 
    } 
} 

class Echo { 
    int count = 0; 
    void hello(){ 
     System.out.println ("helloooooooo.."); 
    } 
} 

輸出

helloooooooo.. 
e1.count is 1 
e2.count is 0 
helloooooooo.. 
e1.count is 2 
e2.count is 2 
helloooooooo.. 
e1.count is 3 
e2.count is 5 
helloooooooo.. 
e1.count is 4 
e2.count is 10 
10 

但是當我改變回聲E2 =新回聲() - 回波E2 = E1,我不明白爲什麼輸出是如此。

public class EchoTestDrive { 
    public static void main(String[] args){ 
     Echo e1 = new Echo(); 
     Echo e2 = e1; 
     int x = 0; 
     while (x<4){ 
      e1.hello(); 
      e1.count = e1.count + 1; 
      if (x==3){ 
       e2.count = e2.count +1; 
      } 
      if(x>0){ 
       e2.count = e2.count + e1.count; 
      } 
      x = x+1; 
      System.out.println("e1.count is " + e1.count); 
      System.out.println("e2.count is " + e2.count); 
     } 
     System.out.println(e2.count); 
    } 
} 

class Echo { 
    int count = 0; 
    void hello(){ 
     System.out.println ("helloooooooo.."); 
    } 
} 

輸出

helloooooooo.. 
e1.count is 1 
e2.count is 1 
helloooooooo.. 
e1.count is 4 
e2.count is 4 
helloooooooo.. 
e1.count is 10 
e2.count is 10 
helloooooooo.. 
e1.count is 24 
e2.count is 24 
24 

我,當x = 0時,e1.count是1和e2.count是0 當x = 1時,e1.count是e1.count是2和e2.count是2.等

我希望有人解釋它。

在此先感謝。

回答

5

當你有Echo e2 = e1;你做到這一點,所以e1和e2都指向相同的內存位置。因此,無論何時添加到e2,它都會添加到該內存位置,因此e1具有相同的值,反之亦然。具體地說

當x = 0

e1.hello(); 
     e1.count = e1.count + 1; //adds 1 to the memory location 
     if (x==3){ // x is 0 so doesn't go in 
      e2.count = e2.count +1; 
     } 
     if(x>0){ // x is 0 so doesn't go in 
      e2.count = e2.count + e1.count; 
     } 
     x = x+1; 
     System.out.println("e1.count is " + e1.count); 
     System.out.println("e2.count is " + e2.count); 
    } 
    System.out.println(e2.count); 
} 

因此,存儲器位置等於1和二者e1和e2是1

當x = 1

e1.hello(); 
     e1.count = e1.count + 1; 
      //adds 1 to the memory location which was already 1 from last time and now equals 2 
     if (x==3){ // x is 1 so doesn't go in 
      e2.count = e2.count +1; 
     } 
     if(x>0){ // x is 1 so goes in as 1 is greater than 0 
      e2.count = e2.count + e1.count; // adds e2 and e1 = 2 + 2 from previous line above = 4 
     } 
     x = x+1; 
     System.out.println("e1.count is " + e1.count); 
     System.out.println("e2.count is " + e2.count); 
    } 
    System.out.println(e2.count); 
} 

因此所述存儲器位置等於4並且e1和e2都是4

3

在Java變量中,保存對象實際上是引用,它們不包含實際值。因此當您編寫e2 = e1時,您將參考e2設置爲指向與e1相同的對象。所以當你寫e2.count = 1時,e1.count被設置爲相同的值,因爲它們是相同對象的字段。

1

做完之後Echo e2 = e1; e1和e2是同一個對象。你只需要兩個手柄來訪問它,但它是一樣的,所有的內容都是一樣的。基本上,您擁有儘可能多的對象,您執行的語句數量爲new

1

Java分配全部參考。因此,當你說

Echo e2 = e1; 

你是說做標記e2另一個參考,並指向相同的數據標記e1參考。然後,當e1指向的數據發生變化時,由e2指向的數據也會發生變化,因爲它與的數據相同。

1

Echo e2 = e1使得e2指的是與e1相同的對象。所以從那以後,你有一個單獨的對象在兩個不同的引用之後。

0

當您設置e1 = e2您是sa因爲參考e1e2指向相同的Echo對象。因此,您應該將e1.counte2.count視爲相同的值。所以它變爲0→1→2→4→5→10 ......等等。