2010-06-22 2 views
2

다음 Java 출력을 이해합니다.Echo e2 = e1 in 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 

그러나 I는 에코 E2 = 새로운 에코를 변경할 때()의 출력은 그래서 이유 I 이해할 수없는, E1 = E2 반향. X = 1, e1.count e1.count 2가되고있는 경우에 저

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 

, e1.count 1 및 e2.count X = 0이고 0 e2.count is 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)와 동일하고, 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가 모두 1

이다 e1과 e2는 모두 4

3

은 오브젝트를 보유하는 Java 변수에서 실제로 참조되며 실제 값을 보유하지 않습니다. 따라서 e2 = e1을 쓸 때는 참조 e2을 설정하여 e1과 동일한 개체를 가리 키도록하십시오. 따라서 e2.count = 1을 쓸 때 e1.count은 같은 개체의 필드이기 때문에 동일한 값으로 설정됩니다.

1

Echo e2 = e1;을 수행 한 후 e1과 e2는 동일한 개체입니다. 액세스 할 수있는 핸들이 두 개 있습니다.하지만 같은 것이고 모든 내용은 같습니다. 기본적으로 실행 한 많은 객체 수만큼 new 문이 있습니다.

1

Java 할당은 모두 참고 용입니다. 따라서, 때 당신은 당신은 e2 표시된 다른 참조를 확인하고 e1 표시된 기준으로 동일한 데이터를 가리 말

Echo e2 = e1; 

을 말한다. 그런 다음 e1이 가리키는 데이터가 변경되면 데이터가이기 때문에 e2이 가리키는 데이터도 변경됩니다.

1

Echo e2 = e1e1과 동일한 객체를 나타냅니다. 그래서 그때부터 두 개의 서로 다른 참조 뒤에 하나의 객체가 있습니다.

0

e1 = e2을 설정하면 sa 참조 번호 e1e2은 동일한 Echo 개체를 가리 킵니다. 따라서 e1.counte2.count을 동일한 값으로 처리해야합니다. 따라서 0 -> 1 -> 2 -> 4 -> 5 -> 10 .. 등