public class Test {


int var;

public Test(int number) {

this.var = number;
}

public static void main (String[] args) {

Test t1 = new Test(100);
Test t2 = new Test(100);

if(t1.equals(t2)) {

System.out.println ("Ok!");

}
else {

System.out.println ("No!");
}


}

}这个结果为啥是“NO”啊???很不理解啊.....

解决方案 »

  1.   

    LZ结贴率太恐怖了
    因为你没有重写equals方法,默认equals跟==一样,比较的是两个对象内存地址是否相等
    t1和t2指向堆中不同的地址,所以是"NO"
      

  2.   

    Test t1 = new Test(100); 
    Test t2 = new Test(100); 
    new 一次内存就会开辟一个内存空间的地址.
    所以这两个内存空间的地址不一样.
      

  3.   

    在Test类继承了Object类中的equals()方法,Object中的方法是比较对象的内存地址
    Test t1 = new Test(100); 
    Test t2 = new Test(100); 

    一个new 就在栈中新建一个对象。所以它们是两个不同的对象。比较时当然不相等啊
      

  4.   

    equals()方法比较的是内存地址,t1和t2是两个不同的对象,当然不等了
      

  5.   

    慢慢就理解了 不理解的时候看看底层代码是怎么实现的(JDK)
      

  6.   

    我在一本Java教程上面看到了一个例子,和这个说明的是一个问题,但更容易迷惑:)String a = new String("hello");
    String b = new String("hello");
    System.out.println(a.equals(b));
    此处输出的是false,因为这里的a和b是两个String的对象,这时候用equals函数比较的是这两个对象的地址,两个对象不同,地址自然不同,这和内容没有关系。另外:
    String a = "hello";
    String b = "hello";
    System.out.println(a.equals(b));
    此处输出的是true,因为这里的a和b是两个普通的变量,和什么对象啊什么的没有关系,就和int a和int b一个道理,这里的equals函数比较的是字符串的内容,自然也就相等了。(以下是个人想法)
    究其原因,可以看一下Sun提供的Java API文档。
    其实问题出在当你使用第一种情况的时候,equals方法是Object类(String的父类)里的方法,在那个方法里面,所有的对象都有equlas方法,比较的都是对象的地址值;而第二种是String类里重写的方法,比较的是String类型变量的内容就是了。不知道这个想法对不对……
      

  7.   

       String a = new String("hello"); 
       String b = new String("hello"); 
       System.out.println(a.equals(b)); 
       
       String c = "hello"; 
       String d = "hello"; 
       System.out.println(c.equals(d)); 
    我运行的时候,那怎么两次输出的结果都是true呢?
      

  8.   

    String a = new String("hello"); 
    String b = new String("hello"); 
    System.out.println(a.equals(b)); 
    此时equals使用的是已经被String类重写了的?
      

  9.   

    楼上的,我运行的也都是true,为什么啊?
      

  10.   

    11楼举得例子是String类的变量,String类已经重写equels方法的缘故
    而楼主举得例子Test类没有重写equels方法,故输出结果为false
      

  11.   

    两种比较方式使用的equels不一样,分别如下:
    1.String a = new String("hello"); 
       String b = new String("hello"); 
       System.out.println(a.equals(b));
    boolean java.lang.String.equals(Object anObject)equals
    public boolean equals(Object anObject)
    Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Overrides:
    equals in class Object
    Parameters:
    anObject - The object to compare this String against 
    Returns:
    true if the given object represents a String equivalent to this string, false otherwise
    See Also:
    compareTo(String), equalsIgnoreCase(String)2.Test t1 = new Test(100);
       Test t2 = new Test(100);
       System.out.println(t1.equals(t2)); 
    boolean java.lang.Object.equals(Object obj)equals
    public boolean equals(Object obj)
    Indicates whether some other object is "equal to" this one. 
    The equals method implements an equivalence relation on non-null object references: 
      

  12.   

    t1=Test@de6ced
    t2=Test@c17164
    这是你的代码中T1和T2的输出结果,
    你的
    Test t1 = new Test(100); 
    Test t2 = new Test(100);
     
    将t1和t2分配了两个内存空间,
    比较的结果肯定不一样了,
      

  13.   

    啊,丢人了,我说错了,String是将equals方法重写了的,所以我举得那两个例子都是true;
    但是如果吧equals语句换成“==”就一个是true一个是false了……一般来说equals都是要重写的,否则直接继承Object类中的equals方法的话,与直接用“==”运算符一样都是比较地址,没什么必要。另外鉴于String在声明类型时的特殊地位,我还联想到了关于Java中基本类型的包装类的问题,这些包装类都直接或间接的继承了Object类,但也都重写了equlas方法……这回我实践了一下,确实是这样……
    道歉道歉……