你这样比较应该是在比较两个对象的reference(引用),这两个string对象的值是相等的都为”aaaa“,但是他们的引用则不相等~

解决方案 »

  1.   

    应该相等,因为你用的是equals比较的是他们的值,你在试试
      

  2.   

    可能snum不是aaaa,带有空格、不可见字符?:)
      

  3.   

    public class Test1
    {
        public static void main(String[]args)
        {
          String snum="aaaa";
          String num="aaaa";
          System.out.println(snum.equals(num));
        }
    }
    测试结果:true;
    确认你的snum与num一致,如多个空格没有.可以把他们trim一下再比较
      

  4.   

    是String类型吗?
    String是重载了public boolean equals(Object anObject)方法的
    你可以看javadoc里面的说明,里面有讲什么时候判断是相等的~~
      

  5.   

    是啊 我也觉得奇怪,测试了一下,好像不是equals函数的问题,我是已经trim过了呢 郁闷阿
      

  6.   

    String snum="aaaa";
    String num="aaaa";
    用equals比较是相等的 因为当执行第2条语句时 系统会去找"aaaa"这个字符串是否有被定义过 如果有(就如上面的情况)则将"aaaa"的引用赋给num 因此snum和num是同一个引用 所以用equals比较是相等的但是如果
    String snum="aaaa";
    String num = new String("aaaa");
    用equals比较就是不相等的 因为new这个关键字会在内存里再创建一个"aaaa"并把引用给num 是一个新的引用 因此snum和num不是同一个引用 所以equals比较是不等的楼主之前是不是有new过 或是snum里多了空格之类的不可见字符
      

  7.   

    public class Test1
    {
        public static void main(String[]args)
        {
          String snum="aaaa";
          //String num="aaaa";
          String num = new String("aaaa");
          System.out.println(snum.equals(num));
        }
    }
    测试结果:true
      

  8.   

    楼上大哥你看api文档吗?
    不要瞎说!!!!!
      

  9.   

    恩 不好意思是我记错了 == 是我说的那样 不是equals
      

  10.   

    楼上的兄弟,这是JDK的源代码:
    /**
         * Compares this string to the specified object.
         * The result is <code>true</code> if and only if the argument is not
         * <code>null</code> and is a <code>String</code> object that represents
         * the same sequence of characters as this object.
         *
         * @param   anObject   the object to compare this <code>String</code>
         *                     against.
         * @return  <code>true</code> if the <code>String </code>are equal;
         *          <code>false</code> otherwise.
         * @see     java.lang.String#compareTo(java.lang.String)
         * @see     java.lang.String#equalsIgnoreCase(java.lang.String)
         */
        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;
        }
      

  11.   

    <%!
         String num="aaaa";
     String snum="aaaa";
     %>
     <html>
     <body>
     <%     if( num.equals(snum)){%>
     num==snum
     <%} else {%>
    num1=snum
    <%} if(num.equals("aaaa")) {%>
       num="aaaa"
     <%} else {%> num!="aaaa" <%}%>
       </body></html>
    结果是相等的.