下面这个程序的结果应该是多少?
public class Asdf
{
public static void main(String[] args)
{
Integer b1 = new Integer ( 1 );
        Integer b2 = new Integer ( 1 );
        Object obj1 = ( Object )b1;
        Object obj2 = ( Object )b2;
if  ( obj1 == obj2 )
     if  ( obj1.equals ( obj2 ) )
          System.out.println ( "a" );
     else
          System.out.println ( "b" );
else
      if  ( obj1.equals ( obj2 ) )
          System.out.println ( "c" );
      else
          System.out.println ( "d" );
}
}

解决方案 »

  1.   

    ==表示两个对象在同一个地址
    equals表示对象的内容相同
      

  2.   

    (obj1 == obj2 )是指两者的内容相等,而obj1.equals ( obj2 )是指两者的内存地址相等。对吗?那Integer b1 = new Integer ( 1 );是指给b1指向内容为1的地址空间还是指向地址为1的空间啊?
      

  3.   

    C ~LZ搞清==和equals的区别~~
      

  4.   

    那为什么不是b啊?难道if  ( obj1 == obj2 )这个条件不成立?
    Integer b1 = new Integer ( 1 );是什么意思啊?
      

  5.   

    结果为c==和equals()两种方式比较:如果测试两个简单类型的树枝是否相等,一定要使用==来比较;如果要比较两个对象的值是否相等,则用对象的equals()来比较;如果需要比较两个引用变量是否指向同一个对象,则用==来比较。此处明显用的是int的封装类Integer分别创建了两个对象b1和b2,指向两个不同对象,而obj1 == obj2 含意是“两个引用变量是否指向同一个对象”对象的值要用equals()方法,很明显两个对象的值是向等的。所以最终结果是c。此段代码改之如下,结果与上会不同的public class Bsdf
    {
    public static void main(String[] args)
    {
    Integer b1 = new Integer ( 1 );
            Integer b2 = b1;
            Object obj1 = ( Object )b1;
            Object obj2 = ( Object )b2;
    if  ( obj1 == obj2 )
         if  ( obj1.equals ( obj2 ) )
              System.out.println ( "a" );
         else
              System.out.println ( "b" );
    else
          if  ( obj1.equals ( obj2 ) )
              System.out.println ( "c" );
          else
              System.out.println ( "d" );
    }
    }
      

  6.   

    这道题主要考察你==与equals的区别
      

  7.   

    lz问:(obj1 == obj2 )是指两者的内容相等,而obj1.equals ( obj2 )是指两者的内存地址相等。对吗?
    答:对于引用型变量obj1和obj2而言,obj1 == obj2 是比较两者的引用地址(也即lz所说的内存地址),equals这里就是比引用对象的内容。另:Integer b1 = new Integer ( 1 );是指让b1指向内容为1的地址空间
      

  8.   

    public class Test{
    public static void main(String[] args){
    String a="s";
    String b=a;
    if(a==b){
    System.out.println("ss");
    }else{
    System.out.println("nn");
    }
    }
    }
    上面代码会输出   ss
    public class Test{
    public static void main(String[] args){
    String a="s";
    String b="s";
    if(a==b){
    System.out.println("ss");
    }else{
    System.out.println("nn");
    }
    }
    }
    上面会输出   nn看了这个LZ应该明白了吧
    ==比较的是引用   equals比较的是内容
      

  9.   

    lastsweetop()  
    写的第二个例子不对哦~~String a="s";
    String b="s";
    其实只是同一个对象~

    String a="s";
    String b=a;
    一样~~
    要写成
    String a=new String("s");
    String b=new String("s"); 
      

  10.   

    For primitive variable, the variable stores the actual values.For reference varialbe, the variable stores the "pointer" to the actual Object. In another word, it stores the way (or the path) to reach the actual Object.Rule 1: "==" compares variable value.For primitive variables, "==" compares the actual values. This is very simple.For reference variables, "==" compares the variable values, which are those "pointers". If object1 == object2, we know that from object1 and object2 we can reach the same actual Object.Rule 2: Functionality of the "equals()" method depend on the class definition.In the very top Object class, it does the same as "==". If a class Foo does not override this method, foo1.equals(foo2) is the same as foo1 == foo2. You can refer to class StringBuffer for one example.