equals()吧,它是String类比较两个字符串是否相等的方法.
==是比较基本数据类型的,还有对象是不是null时用.
==是比较基本数据类型的,还有对象是不是null时用.
解决方案 »
- 如何修改fillrect和clearrect在快速拖动的时候出现间断的问题?
- 不知道该怎么问这个问题了.关于classpath想add进来font的问题!
- 问一个关于文件输出流比较奇怪的问题关于FileInputStream和FileOutputStream的.
- 程序执行到pool.notifyAll()时出现错误!在线等待
- 100---------JScrollPane中的滚动条为何不行啦?熟悉swing控件的高手请让我送点分
- 请问Class.forName的参数可不可以是绝对路径!
- 郁闷呀~怎么都要求工作经验!
- 怎么样更换窗体上的图标?
- 收邮件时我怎么连接不上邮件服务器?
- 困扰很久的InputStreamReader和OutputStreamWriter
- 如何调控JButton的大小?
- 怎么调用的?
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation: --It is reflexive: for any reference value x, x.equals(x) should return true.
--It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
--It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
--It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true). Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. 请注意其中对==的描述“if x and y refer to the same object (x==y has the value true)”这就说明==只比较句柄(相当于c++中的指针、地址)的值了参看如下程序片断,可以说明在String中慎用两者:
...
String str11 = "12345";
String str12 = new String("12345");
if(str11 == str12)
System.out.println("str11 == str12");
else
System.out.println("str11 != str12");
if(str11.equals(str12))
System.out.println("str11 equals str12");
else
System.out.println("str11 does not equals str12");
...输出结果:
str11 != str12
str11 equals str12
equeals 才是逻辑上的相等.比较两个对象是不是一样.
而equals要看具体实现
一般可实现为比较具体内容
如果你要比较两个对象是否一致,用equals
如果你要比较是不是同一个对象,可以用==
┏━★━━◆━━★━┓
♂欢|◢CSDN◣|使♂ ▲自由保存帖子,浏览,关注检测
┃迎|◥论坛助手◤|用┃ ▲完善的CSDN客户端工具
┗━☆━━◇━━━☆┛ ▲自动添加签名......让你更快,更爽,更方便地上CSDN...
http://www.csdn.net/expert/topic/573/573604.xml
http://www.chinaok.net/csdn/csdn.zip
String a="abc";
String b="abc";
System.out.println(a==b); // true
System.out.println(a.equals(b)); // true
a=new String("abc");
b=new String("abc");
System.out.println(a==b); // false
System.out.println(a.equals(b)); //true
对于两个对象是判断是否是同一个对象(地址是否相同)。equal()判断两个对象是否值相同,用户可以重载来定义自己的判断规则。
通常字符串比较时都用equals== 既可以比较对象又可以比较基本数据类型(23,2.33 之类的东西),比较对象时一定是比较句柄(引用),比较基本数据类型时比较值。
例如:
string1=kangta;
string2=kang;
string3=ta;
string4=string2+string3;
然后比较string1==string4
结果会返回fals
不知道小弟我刚学java3个月,说的对不对,如不对的地方,请前辈们指点!程序上写的不太好,主要是老妈不让上网,时间仓促啊^_^
equeals 才是逻辑上的相等.比较两个对象的内容是不是一样.
class TestEquals {
public static void main(String[] args) {
String s = "hello";
String t1 = "hel";
String t2 = "lo";
String t = t1+t2;
String s1 = "hello";
char c[] = {'h','e','l','l','o'} ;
System.out.println("s="+s);
System.out.println("t="+t);
System.out.println("1.s==\"hello\" "+(s=="hello"));
System.out.println("2.s.equals(\"hello\") "+(s.equals("hello")));
System.out.println("3.s.equals(t1+t2) "+(s.equals(t1+t2)));
System.out.println("4.s==t "+(s==t));
System.out.println("5.s==\"hel\"+\"lo\" "+(s=="hel"+"lo"));
System.out.println("6.s==t1+t2 "+(s==t1+t2));
System.out.println("7.s==t1+\"lo\" "+(s==t1+"lo"));
System.out.println("8.s.equals(t) "+(s.equals(t)));
System.out.println("9.t.equals(c) "+(t.equals(c)));
System.out.println("10.t.equals(new String(\"hello\")) "+(t.equals(new String("hello"))));
System.out.println("11.s.equals(s1) "+(s.equals(s1)));
System.out.println("12.s==s1 "+(s==s1));
//System.out.println("s==c "+(s==c));
}
}
equals()方法可以自己写以满足相等的比较