public class name
{
  public static void main(String args[])
  {
    String s="true";
    Boolean b=new Boolean("true");
    if(s.equals(b))
      System.out.println("yes");
    else
       System.out.println("no");
  }
}
请问:这段代码的输出结果是什么,为什么,希望您在给出结果的时候附上您的理解,您是如何想的,您是如何理解的,谢谢!!

解决方案 »

  1.   

    nos是String类型,b是Boolean,类型不一致
      

  2.   

    结果是no
    s是String类型的,调用String类中的equals()public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
    char v1[] = value;
    char v2[] = anotherString.value;
    int i = offset;
    int j = anotherString.offset;
    while (n-- != 0) {
        if (v1[i++] != v2[j++])
    return false;
    }
    return true;
        }
    }
    return false;
        }先执行到if (this == anObject), 跳过if,因为s和b地址不同
    再执行到if (anObject instanceof String),跳过if,因为b不是String类型的对象
    最后return false,返回false,打印no
      

  3.   

       这个题确实不错,对粗心的人来说是一个警钟!我也补充了一下代码:public class Name 

      public static void main(String args[]) 
      { 
        String s="true"; 
        Boolean b=new Boolean("true");
        String str=new String("true"); 
        if(s.equals(b)) 
          System.out.println("yes"); 
        else 
          System.out.println("no");
        if(s.equals(str)) 
          System.out.println("yes"); 
        else 
          System.out.println("no");
        if(s==str) 
          System.out.println("yes"); 
        else 
          System.out.println("no");             
      } 
    }
    输出结果:no
             yes
             no