网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.新手入门之SpringBeanDoc使用.
.EJB 3.0开发指南之多表映射.
.多形对象的互换使用.
.我与Java的故事-从这里开始.
.Sun 发布 JAXP 1.
.X3D实战基础讲座之二.
.2005Java中国开发者大会即将举行.
.Swing简介.
.一个简单的代理服务器.
.调试器--jdb.exe.
.用SAAJ1.2发送接收二进制Web服务.
.javac-----Java编程语言编译器.
.Servlet 中如何捕获 Session 事件.
.如何学好J2ME.
.Java开发环境的过去、现在和将来.
.httpclient3.0 上传文件名及fiel.
.Java解析网络数据流的三种特殊方.
.实战SWT布局.
.java的一些规则.
.Java、XML与数据库编程实践 (二).

equals与“==”操作符的比较

发表日期:2008-1-5



  equals与"=="操作符的比较

--------------------------------------------------------------------------------



equals方法是Object类的一个方法,所有继续自Object类的类都会集成此方法,并且可以重载这个方法来实现各自的比较操作,而且jdk也正是推荐这种做法。所以开发人员尽可以在自己的类中实现自己的equals方法来完成自己特定的比较功能,所以各个类的equals方法与= =之间并没有绝对的关系,这要根据各自类中自己的实现情况来看。也就是说可能会有两种情况发生:equals方法和= =相同或者不相同。在多数情况下这两者的区别就是究竟是对对象的引用进行比较还是对对象的值进行比较(其他非凡情况此处不予考虑)。那么= =操作符是比较的什么呢?= =操作符是比较的对象的引用而不是对象的值。并且由下面的源代码可以看出在最初的Object对象中的equals方法是与= =操作符完成功能是相同的。
源码:
Java.lang.Object.equals()方法:
-------------------------------------------------------------
public boolean equalss(Object obj) {
return (this = = obj);
}
-------------------------------------------------------------
jdk文档中给出如下解释:
-------------------------------------------------------------
The equalss method implements an equivalence relation:
? It is reflexive: for any reference value x, x.equalss(x) should return true.
? It is symmetric: for any reference values x and y, x.equalss(y) should return true if and only if y.equalss(x) returns true.
? It is transitive: for any reference values x, y, and z, if x.equalss(y) returns true and y.equalss(z) returns true, then x.equalss(z) should return true.
? It is consistent: for any reference values x and y, multiple invocations of x.equalss(y) consistently return true or consistently return false, provided no information used in equalss comparisons on the object is modified.
? For any non-null reference value x, x.equalss(null) should return false.
The equalss 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 equals objects must have equals hash codes.
-------------------------------------------------------------
由以上的注释可知equals方法和 = =操作符是完成了相同的比较功能,都是对对象的引用进行了比较。那么我们熟悉的String类的equals方法是对什么内容进行比较的呢?下面我们来看它的代码和注释:
源代码:
-------------------------------------------------------------
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;
}

-------------------------------------------------------------
此方法的注释为:
-------------------------------------------------------------
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
-------------------------------------------------------------
由上面的代码和注释可以得到String类的equal方法是对对象的值进行比较。
根据以上的讨论可以得出结论:equal方法和= =操作符是否存在区别要个别对待,要根据equal的每个实现情况来具体判定。
*******************************

belter(belter@sina.com)

none.blogdriver.com
上一篇:Forte for Java简介与基础安装 人气:684
下一篇:Effective java学习笔记8 人气:640
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐