码迷,mamicode.com
首页 > 编程语言 > 详细

java中用equals比较两个内容相同的字符数组

时间:2014-08-23 16:38:21      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:des   java   os   io   for   ar   问题   on   ef   

********************************************
你在数组上调用函数equals,比较的是c和ch的地址
改成
if(Arrays.equals(ch,c));
就可以比较c和ch的内容了
********************************************

java.sun.com上说,
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null 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).
It doesn‘t perform an intelligent comparison for most classes unless the class overrides it. It has been defined in a meaningful way for most Java core classes. If it‘s not defined for a (user) class, it behaves the same as ==.

"If it‘s not defined for a (user) class, it behaves the same as =="由于你没有重写equals方法,所以它比较的是数组的地址,相当于是==。

另外,有问题可以去sun.java.com上找每个class的说明,也可以装个Netbeans java版(开源的),Netbeans 直接提供每个方法的说明
望采纳

java中用equals比较两个内容相同的字符数组

标签:des   java   os   io   for   ar   问题   on   ef   

原文地址:http://www.cnblogs.com/kaikailele/p/3931371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!