EffieR

          最符合理智的莫過(guò)于對(duì)理智的否定

           

          equals & ==

          1.? 先了解:string a=new string("EffieR"); 表示一定要分配內(nèi)存string對(duì)象,還有相應(yīng)的引? 用。string b="EffieR",此時(shí)就不再分配內(nèi)存,而是建立一個(gè)新的引用b指向同一個(gè)對(duì)象"EffieR".
          ?
          例如:
          public class TestOne
          ?{
          ??? public static void main(String[] args) {
          ??????? String s1 = "Monday";
          ??????? String s2 = "Monday";
          ??????? if (s1 == s2)
          ??????????? System.out.println("s1 == s2");
          ??????? else
          ??????????? System.out.println("s1 != s2");
          ??? }
          }


          2. 如果是兩個(gè)新的對(duì)象(new),內(nèi)存肯定不同,那么引用比較時(shí)也不相同。
          ??? 而調(diào)用equals時(shí)則是比較對(duì)象的內(nèi)容,可實(shí)現(xiàn)我們的內(nèi)容比較。
          例如:
          public class? Testtwo
          {
          ?public static void main(String[] args)
          ?{
          ?? String a=new String("foo");
          ???????? String b=new String("foo");
          ??
          ?? System.out.println("==:"+ (a==b) );
          ?? System.out.println("equals: "+ a.equals(b));
          ??
          ??
          ?}
          }

          3. string.intern(); 來(lái)釋放相同值的string內(nèi)存

          例如:
          public class TestThree
          {

          ?/**
          ? * @param args
          ? */
          ?public static void main(String[] args)
          ?{
          ??// TODO Auto-generated method stub
          ?? String a="foo";
          ??
          ???????? String b=new String("foo").intern();
          ??
          ?? System.out.println("==:"+ (a==b) );
          ?? System.out.println("equals: "+ a.equals(b));
          ??

          ?}

          }

          4. 測(cè)試直接繼承Object的方法equals()

          ?例如:
          class testEquals
          {
          ?testEquals()
          ?{
          ??System.out.println("testEquals object");
          ?}

          };
          ?
          public class? TestFour
          {
          ?public static void main(String[] args)
          ?{
          ??
          ??
          ???????? testEquals e1=new testEquals();
          ?? testEquals e2=new testEquals();
          ?? System.out.println(e1.equals(e2));
          ???
          ?}
          }

          5. 創(chuàng)建自己的類,覆蓋equals()


          例如:

          class testEquals2
          {
          ??? private int a;
          ?testEquals2(int p)
          ?{
          ??
          ??a=p;
          ??
          ?}
          ?public int getMember()
          ?{
          ??return this.a;
          ?}
          ?public boolean equals(testEquals2 ob)
          ?{
          ??int a,b;
          ??a=this.getMember();
          ??b=ob.getMember();
          ???? return a==b;
          ?}
          };

          public class TestFive
          {
          ?public static void main(String[] args)
          ?{
          ????? testEquals2 e3=new testEquals2(11);
          ?? testEquals2 e4=new testEquals2(11);
          ?? System.out.println(e3.equals(e4));
          ?}
          }

          6...

          posted on 2006-04-29 13:38 EffieR 閱讀(638) 評(píng)論(0)  編輯  收藏 所屬分類: 學(xué)習(xí)和技術(shù)


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          相冊(cè)

          deep world

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 聂荣县| 莲花县| 思茅市| 内丘县| 尼勒克县| 周至县| 龙口市| 宜兰县| 桃江县| 富宁县| 仲巴县| 高尔夫| 滦平县| 武义县| 舒城县| 呈贡县| 紫云| 临汾市| 页游| 黄大仙区| 遵化市| 正镶白旗| 洪泽县| 大埔区| 连州市| 锦州市| 西乌| 刚察县| 界首市| 汉阴县| 库伦旗| 新津县| 迁安市| 乐平市| 阳江市| 吴堡县| 麻栗坡县| 清流县| 慈溪市| 赤水市| 察隅县|