你这样比较应该是在比较两个对象的reference(引用),这两个string对象的值是相等的都为”aaaa“,但是他们的引用则不相等~
解决方案 »
- 遇到一个奇怪的问题,访问带上工程名时路径问题
- 求解一道JavaWeb笔试题
- 我想买个虚拟主机 不知道哪里卖的支持北京网通,电信的速度比较好 请有经验的人介绍下,谢谢!
- 短信发送程序
- 在线求助:JSP页面如何调用JAR包文件啊!
- 数据库的问题????
- 请问大家一般是用什么方式管理自己收集和编写的代码,用到什么软件呢,大家探讨一下:)
- JSP中有关“随机数”的问题?大家来帮帮我好吗?谢谢!(在线等待)
- 面试问题,怎么办
- 请问各位大虾,小弟今天开始学jsp了,这学期我们有java课,所以已经下载了jdk(好象是1.2),请问我的98环境怎么配置jsp环境呀?我的jdk可以运行.java程序,别的我就不知道了....谢谢!
- 企业考勤管理的需求
- servlet的一个小问题,急!
{
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>
结果是相等的.