+ -
当前位置:首页 → 问答吧 → 关于Equals方法???

关于Equals方法???

时间:2011-10-18

来源:互联网

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是同一个引用?

作者: owo_lee   发布时间: 2011-10-18

true

作者: DongAoYuan   发布时间: 2011-10-18

还有
public class Test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
 
System.out.println(s1==s3);  
System.out.println(s1.equals(s2)); 
System.out.println(s1.equals(s3)); 
System.out.println(s2.equals(s3));
  }
}
这句:System.out.println(s1==s3);一个常量和一个对象能比较?

作者: owo_lee   发布时间: 2011-10-18

看你结帖率,记得结贴给分哈

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

作者: ioe_gaoyong   发布时间: 2011-10-18

字符串本身也是对象,s1 s3都是字符串对象的引用,肯定可以比较
引用 2 楼 owo_lee 的回复:

还有
public class Test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");

System.out.println(s1==s3);
System.out.println(s1.equa……

作者: ioe_gaoyong   发布时间: 2011-10-18

逛了两天的CSDN 这结帖率 太坑爹了 惨不忍睹啊~~~

作者: nizhicheng   发布时间: 2011-10-18

热门下载

更多