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
楼上大哥你看api文档吗? 不要瞎说!!!!!
恩 不好意思是我记错了 == 是我说的那样 不是equals
楼上的兄弟,这是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; }
{
public static void main(String[]args)
{
String snum="aaaa";
String num="aaaa";
System.out.println(snum.equals(num));
}
}
测试结果:true;
确认你的snum与num一致,如多个空格没有.可以把他们trim一下再比较
String是重载了public boolean equals(Object anObject)方法的
你可以看javadoc里面的说明,里面有讲什么时候判断是相等的~~
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里多了空格之类的不可见字符
{
public static void main(String[]args)
{
String snum="aaaa";
//String num="aaaa";
String num = new String("aaaa");
System.out.println(snum.equals(num));
}
}
测试结果:true
不要瞎说!!!!!
/**
* 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;
}
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>
结果是相等的.