West Farm
          吾本布衣,生于川北,躬耕于代碼的田地上。
          posts - 16,  comments - 15,  trackbacks - 0
          原文地址:http://stackoverflow.com/questions/15496/hidden-features-of-java
          我選擇幾個有趣的:

          1. 
          Double Brace Initialization 
          private static final Set<String> VALID_CODES = new HashSet<String>() {{
              add(
          "XZ13s");
              add(
          "AB21/X");
              add(
          "YYLEX");
              add(
          "AR2D");
           }};

          2. 泛型參數可以這么用
          public static <A, B extends Collection<A> & Comparable<B>> boolean foo(B b1, B b2, A a) {
             
          return (b1.compareTo(b2) == 0) || b1.contains(a) || b2.contains(a);
          }

          3. Check null is not 
          necessary
          if( null != aObject && aObject instanceof String )
          {
              // not necessary to check != null

          }
           just use:
          if( aObject instanceof String )
          {
             
          ...
          }


          4. 
          Allowing methods and constructors in enums
          enum Cats {
            FELIX
          (2), SHEEBA(3), RUFUS(7);

           
          private int mAge;
           
          Cats(int age) {
              mAge
          = age;
           
          }
           
          public int getAge() {
             
          return mAge;
             
          }
          }

          5. 自Java 6開始在聲明類路徑時可以使用通配符 
          java -classpath ./lib/* so.Main
          6. Assert

          assert object != null;
          posted on 2011-10-12 13:11 West Farmer 閱讀(371) 評論(2)  編輯  收藏 所屬分類: Java

          FeedBack:
          # re: Java中鮮為人知的功能
          2011-10-12 20:53 | @joe
          確實用的少。  回復  更多評論
            
          # re: Java中鮮為人知的功能
          2011-10-12 23:00 | 蜀山兆孨龘
          雙花括號初始化那個,會生成一個匿名內部類,且可能嚴重影響 equals:如果 equals 用 if (getClass() == other.getClass()) 檢查類型,就會始終返回 false,顯然此處 VALID_CODES.getClass() != HashSet.class。個人不推薦。  回復  更多評論
            

          <2011年10月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 科尔| 田阳县| 当阳市| 砚山县| 陇南市| 星座| 寿阳县| 抚宁县| 固安县| 河北省| 革吉县| 古田县| 威宁| 禹州市| 嘉善县| 巴青县| 建瓯市| 镇坪县| 龙岩市| 德庆县| 荃湾区| 兴国县| 云龙县| 宁蒗| 凤山市| 乌海市| 安阳县| 威远县| 南溪县| 雅江县| 惠州市| 米易县| 清新县| 汝城县| 临邑县| 芮城县| 黄浦区| 临沂市| 门头沟区| 贺兰县| 娄底市|