日期:2014-05-20  浏览次数:20583 次

关于Equals方法???
class TestDog {
String name;
String says;
}

public class Dog {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

TestDog dog1 = new TestDog();
TestDog dog2 = new TestDog();
TestDog dog3 = new TestDog();
  dog3 = dog1;
  System.out.println(dog3 == dog1);
  }
}

dog3 和dog1是同一个引用?

------解决方案--------------------
看你结帖率,记得结贴给分哈

你代码里面执行的dog3=dog1; 就是将dog1对象引用赋值给dog3,这样dog3 dog1同时引用到原来dog1引用的对象,若以dog3==dog1结果为true ,把dog3=dog1去掉就是false


------解决方案--------------------
你这就错了
String s1 = "abc"; 
string s = new String("abc");
s1==s //false

String s1 = "abc"; 
string s = "abc";
s1==s //true



探讨
字符串赋值String s1 = "abc"; 只是字符串对象 string s = new String("abc")的另一种方式而已 实质上是一样的 都是对象

------解决方案--------------------
你代码里面执行的dog3=dog1; 就是将dog1对象引用赋值给dog3,这样dog3 dog1同时引用到原来dog1引用的对象,若以dog3==dog1结果为true ,把dog3=dog1去掉就是false

------解决方案--------------------
探讨

你这就错了
String s1 = "abc";
string s = new String("abc");
s1==s //false

String s1 = "abc";
string s = "abc";
s1==s //true



引用:
字符串赋值String s1 = "abc"; 只是字符串对象 string……

------解决方案--------------------
equals(Object object) 参数为Object类型的引用,上面分别创建了三个不同的对象,dog1,dog2,dog3分别指向这三个对象,当dog3=dog1时,dog3就不在指向原来的对象了,因为对象的引用在相同时刻只能指向一个对象,所以此时dog3和dog1都指向原来dog1所指向的对象。。。。。。
希望对楼主有所帮助!
------解决方案--------------------
TestDog dog1 = new TestDog();
 TestDog dog2 = new TestDog();
 TestDog dog3 = new TestDog();
dog3 = dog1;
System.out.println(dog3 == dog1);

}
输出 true 
dog3本来是指向自己的对象, dog3 = dog1执行完成以后
会把dog1所引用的地址赋给dog3
这样执行完成以后,dog1 dog3会指向同一块堆空间
所以,会输出true