不理解Java中的Echo e2=e1
我了解以下Java输出不理解Java中的Echo e2=e1,java,Java,我了解以下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;
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..");
}
}
但是,当我将Echo e2=new Echo()更改为Echo 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..");
}
}
对我来说,当x=0时,e1.count为1,e2.count为0。
当x=1时,e1.count为e1.count为2,e2.count为2。等等
我希望有人能解释一下
提前感谢。在Java中,包含对象的变量实际上是引用,它们不包含实际值。因此,当您编写
e2=e1
时,您将referencee2
设置为指向e1
所指向的同一对象。因此,当您写入e2.count=1
时,e1.count
被设置为相同的值,因为它们是相同对象的字段。在您写入Echo e2=e1之后代码>e1和e2是同一对象。您只有两个句柄来访问它,但它是相同的,所有内容都是相同的。基本上,您有尽可能多的对象,因为您执行的新语句都是通过引用的。因此,当你说
Echo e2 = e1;
您的意思是创建另一个标记为e2
的引用,并将其指向与标记为e1
的引用相同的数据。然后,当e1
指向的数据发生变化时,e2
指向的数据也会发生变化,因为它是相同的数据 Echo e2=e1
使e2
引用与e1
相同的对象。从那时起,在两个不同的引用后面有一个对象。当你有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);
}
因此,当您设置e1=e2
时,内存位置等于4,并且e1和e2都是4您的意思是,引用e1
和e2
指向相同的Echo
对象。因此,您应该将e1.count
和e2.count
视为相同的值。所以它变成了0->1->2->4->5->10。。等等
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);
}
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);
}