if(" String ".trim() == "String")  //trim()去掉空格,字符串判断相等
                                   //用equals("String"),其它类似
System.out.println("Equal");
else
System.out.println("Not Equal"); //Not Equal

解决方案 »

  1.   

    ==是判断对象相等的
    字符串不能改变改变字符串可用StringBuffer类
    第一个不等因为" String "和String不是同一个对象 " String "前面有空格
    第二个相等因为"String"是同一个对String象
    第三个相等因为subString不能改变字符串 所以string这个String对象没有变
      

  2.   

    第二、第三个例子中
    前"STRING"和后"STRING",前"String"和后"String"是同一对象实例,所以它们的引用相等。
    而第一个例子中" String "和"String"不是同一个对象实例,所以不相等注意:判断对象是否是同意对象用==,判断对象值是否相等用equal()
    如下例:
    if(" String ".trim().equals("String"))
    System.out.println("Equal");
    else
    System.out.println("Not Equal"); //Equalif( "string".toUpperCase() == "STRING") 
    System.out.println("Equal"); 
    else 
    System.out.println("Not Equal"); //NOT Equalif( "string".toUpperCase().equals("STRING")) 
    System.out.println("Equal"); 
    else 
    System.out.println("Not Equal"); //Equal
      

  3.   

    TO: jokerjava(冷血)我觉得你的第一和第三点说的有矛盾。
    看看jdk的源代码:
             public String trim() {
    int len = count;
    int st = 0;
    int off = offset;      /* avoid getfield opcode */
    char[] val = value;    /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) {
        st++;
    }
    while ((st < len) && (val[off + len - 1] <= ' ')) {
        len--;
    }
    return ((st > 0) || (len < count)) ? substring(st, len) : this;
        }
    可以看到,trim方法也是调用了substring方法的。如果第三点象你说的那样,那么trim也应该是这样
      

  4.   

    对同一常量字符串的引用是同样的。
    String a = "abc";
    String b = "abc";  则 a ==b 是成立的
    可是如果
    String a = new String("abc");
    String b = new String("abc"); 则 a==b是不成立的
      

  5.   

    knight12说得很好!用==不能说明两者是否相同,应该是用函数~~~~~~!
      

  6.   

    字符串内容的比较用 equals若是查看两个指针是否指向同一个对象 才是 ==
      

  7.   

    Strings have the special characteristic that if a program has several String objects that contain identical character sequences, then those String objects all map to the same memory.public class Test {
        public static void main(String[] args) {
            String s1 = "aaa";
            String s2 = "aaa";
            System.out.println(s1 == s2);
        }
    }
      

  8.   

    代码就懒得分析了。对Object,  == 比较的是二者是不是对同一Object的Reference;
                equales() 比较的是二者的content是否一致。
      

  9.   

    把你所有的==换成
    equals。==比较的是两个obj reference。
    equals是比较两个字符串的内容。
      

  10.   

    String a = "String";
    String b = "String";  则 a ==b 是成立的
    a和b都只向"String"对象
    所以他们相等
    String s = " String ";
    String tmp = s.trim();
    tmp == s;他们都指向" String "对象 所以不等于"String"对象String s = "String";
    String tmp = s.trim(); 
    tmp == s == "String"
    所以("String".substring(0) == "String")是成立的
      

  11.   

    api  中trim、substring和toUpperCase方法中,
    如果不需要变换的话是不会改变对象的,如
    果需要变换都是会返回个new String()的。
    而如果对象是new String()生成的,就是个
    新的对象。和原来的String是不同的。所以:
    " String ".trim() != "String"
    "STRING".toUpperCase() == "STRING"
    "String".substring(0) == "String"
      

  12.   

    ‘==’只有是同一个对象时才成立(指向同一内存)
    ‘.equals()’才能比较对象的值
      

  13.   

    equals()比较内容是深度比较么?