﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Huion-文章分类-J2SE</title><link>http://www.blogjava.net/yihui/category/1471.html</link><description>&lt;font face="宋体" size="2"&gt;&lt;strong&gt;事业向左...爱情向右...失去的...我会在未来期待...&lt;/strong&gt;&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 07:43:54 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 07:43:54 GMT</pubDate><ttl>60</ttl><item><title>J2SE基础知识之不要依懒Object.equals();</title><link>http://www.blogjava.net/yihui/articles/6411.html</link><dc:creator>一辉</dc:creator><author>一辉</author><pubDate>Mon, 20 Jun 2005 15:47:00 GMT</pubDate><guid>http://www.blogjava.net/yihui/articles/6411.html</guid><wfw:comment>http://www.blogjava.net/yihui/comments/6411.html</wfw:comment><comments>http://www.blogjava.net/yihui/articles/6411.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/yihui/comments/commentRss/6411.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yihui/services/trackbacks/6411.html</trackback:ping><description><![CDATA[问题提出：看下面代码<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_21_335_Open_Image onclick="this.style.display='none'; Codehighlighter1_21_335_Open_Text.style.display='none'; Codehighlighter1_21_335_Closed_Image.style.display='inline'; Codehighlighter1_21_335_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_21_335_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_21_335_Closed_Text.style.display='none'; Codehighlighter1_21_335_Open_Image.style.display='inline'; Codehighlighter1_21_335_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Computer</SPAN><SPAN id=Codehighlighter1_21_335_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_21_335_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>　　</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;cpuInfo;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>　　</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;diskInfo;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>　　<BR><IMG id=Codehighlighter1_128_191_Open_Image onclick="this.style.display='none'; Codehighlighter1_128_191_Open_Text.style.display='none'; Codehighlighter1_128_191_Closed_Image.style.display='inline'; Codehighlighter1_128_191_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_128_191_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_128_191_Closed_Text.style.display='none'; Codehighlighter1_128_191_Open_Image.style.display='inline'; Codehighlighter1_128_191_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>　　</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Computer(String&nbsp;cpuInfo,String&nbsp;diskInfo)</SPAN><SPAN id=Codehighlighter1_128_191_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_128_191_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　 </SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.cpuInfo&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;cpuInfo;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;　　　</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.diskInfo&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;diskInfo;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;　　}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_217_316_Open_Image onclick="this.style.display='none'; Codehighlighter1_217_316_Open_Text.style.display='none'; Codehighlighter1_217_316_Closed_Image.style.display='inline'; Codehighlighter1_217_316_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_217_316_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_217_316_Closed_Text.style.display='none'; Codehighlighter1_217_316_Open_Image.style.display='inline'; Codehighlighter1_217_316_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>　　</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;toInfo()</SPAN><SPAN id=Codehighlighter1_217_316_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_217_316_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>　　　　</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;StringBuffer(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cpu:</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).append(cpuInfo).append(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">disk:</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).append(diskInfo).toString();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>　　　}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">other&nbsp;methods</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_355_608_Open_Image onclick="this.style.display='none'; Codehighlighter1_355_608_Open_Text.style.display='none'; Codehighlighter1_355_608_Closed_Image.style.display='inline'; Codehighlighter1_355_608_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_355_608_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_355_608_Closed_Text.style.display='none'; Codehighlighter1_355_608_Open_Image.style.display='inline'; Codehighlighter1_355_608_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Test</SPAN><SPAN id=Codehighlighter1_355_608_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_355_608_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_398_606_Open_Image onclick="this.style.display='none'; Codehighlighter1_398_606_Open_Text.style.display='none'; Codehighlighter1_398_606_Closed_Image.style.display='inline'; Codehighlighter1_398_606_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_398_606_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_398_606_Closed_Text.style.display='none'; Codehighlighter1_398_606_Open_Image.style.display='inline'; Codehighlighter1_398_606_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String&nbsp;[]&nbsp;args)</SPAN><SPAN id=Codehighlighter1_398_606_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_398_606_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>　　Computer&nbsp;c1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Computer(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">AMD-XP1800</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">IBM-3002</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>　　Computer&nbsp;c2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Computer(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">AMD-XP1800</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">IBM-3002</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG id=Codehighlighter1_531_604_Open_Image onclick="this.style.display='none'; Codehighlighter1_531_604_Open_Text.style.display='none'; Codehighlighter1_531_604_Closed_Image.style.display='inline'; Codehighlighter1_531_604_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_531_604_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_531_604_Closed_Text.style.display='none'; Codehighlighter1_531_604_Open_Image.style.display='inline'; Codehighlighter1_531_604_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp; &nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(c1.equals(c2))</SPAN><SPAN id=Codehighlighter1_531_604_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_531_604_Open_Text><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp; 　　　System.</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">c1==c2</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;　</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;　　　 System.</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">c1!=c2</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top></SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>　　}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></DIV><BR><BR>代码的运行结果为：c1!=c2。一开始感觉应该有点意外吧！这个例子告诉我们，不要依赖Object的默认实现，Object 提供的equals方法如下：<BR><BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG id=Codehighlighter1_33_58_Open_Image onclick="this.style.display='none'; Codehighlighter1_33_58_Open_Text.style.display='none'; Codehighlighter1_33_58_Closed_Image.style.display='inline'; Codehighlighter1_33_58_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_33_58_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_33_58_Closed_Text.style.display='none'; Codehighlighter1_33_58_Open_Image.style.display='inline'; Codehighlighter1_33_58_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;boolean&nbsp;equals(Object&nbsp;obj)</SPAN><SPAN id=Codehighlighter1_33_58_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_33_58_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>　</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;obj);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></DIV><BR><BR>Object 的= =运算只进行Object的引用的比较，也就是内存地址的比较！<BR><BR>现在想一想，要是一开始不注意的话，那结果是多么愚蠢吧？<img src ="http://www.blogjava.net/yihui/aggbug/6411.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yihui/" target="_blank">一辉</a> 2005-06-20 23:47 <a href="http://www.blogjava.net/yihui/articles/6411.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>