從code學習java tiger之自動裝箱 拆箱

          Posted on 2006-06-22 18:34 BlueO2 閱讀(403) 評論(0)  編輯  收藏 所屬分類: JAVA foundation
          public ? class ?AutoBoxing? {
          ????
          ????
          /** ?Creates?a?new?instance?of?AutoBoxing? */
          ????
          public ?AutoBoxing()? {
          ????}

          ????
          public ? void ?boxingDemo() {
          ????????
          // auto?boxing
          ????????Integer?i? = ? 0 ;
          ????????
          float ?f? = ? 1.66f ;
          ????????Float?F?
          = ?f;
          ????????
          // auto?unboxing
          ????????Integer?I? = ? new ?Integer( 1 );
          ????????
          int ?i2? = ?I;
          ????????
          // null?value?test,?it?will?case?NullPointerException
          ????????Integer?I2? = ? null ;
          ????????
          int ?i3? = ?I2;
          ????}

          ????
          public ? void ?testOperator() {
          ????????Integer?i?
          = ? 1 ;
          ????????
          while ( true ) {
          ????????????i
          ++ ;
          ????????????System.out.println(
          " Counter: " + i);
          ????????????
          if (i > 5 )? break ;
          ????????}

          ????}

          ????
          public ? void ?testCompare() {
          ????????
          // it's?equal?because?-127~127?are?immutable?objects
          ????????Integer?i? = ? 1 ;
          ????????Integer?i2?
          = ? 1 ;
          ????????
          if (i? == ?i2)?System.out.println( " 1:Equal " );
          ????????
          else ?System.out.println( " 1:Not?Equal " );
          ????????
          // it's?not?equal?because?j?and?j2?are?different?objects
          ????????Integer?j? = ? 200 ;
          ????????Integer?j2?
          = 200 ;
          ????????
          if (j? == ?j2)?System.out.println( " 200:Equal " );
          ????????
          else ?System.out.println( " 200:Not?Equal " );
          ????}

          ????
          public ? void ?testControl() {
          ????????Boolean?flag?
          = ? true ;
          ????????Integer?i?
          = ? 20 ;
          ????????Integer?j?
          = ? 30 ;
          ????????
          if (flag) {
          ????????????System.out.println(
          " Boolean?affects " );
          ????????}

          ????????
          if (i < j)
          ????????????System.out.println(
          " Integer?affects " );
          ????}

          ????
          public ? void ?testMethod( double ?arg) {
          ????????System.out.println(
          " public?void?testMethod(double?arg)?is?invoked " );
          ????}

          ????
          public ? void ?testMethod(Integer?arg) {
          ????????System.out.println(
          " public?void?testMethod2(Integer?arg)?is?invoked " );
          ????}

          ????
          public ? static ? void ?main(String?args[]) {
          ????????AutoBoxing?auto?
          = ? new ?AutoBoxing();
          ????????auto.testCompare();
          ????????auto.testOperator();
          ????????auto.testControl();
          ????????
          int ?i? = ? 1 ;
          ????????
          // ?public?void?testMethod(Integer?arg)?wouldn't?be?invoked
          ????????
          // because??public?void?testMethod(double?arg)?will?be?invoked?in?JDK1.4
          ????????
          // Java?tiger?consider?the?backward?capability
          ????????auto.testMethod(i);
          ????????auto.boxingDemo();
          ????}

          ????
          }

          posts - 29, comments - 3, trackbacks - 0, articles - 0

          Copyright © BlueO2

          主站蜘蛛池模板: 尼玛县| 漳州市| 湘西| 甘德县| 罗定市| 乌兰察布市| 磐安县| 孝感市| 宁南县| 镇安县| 黔江区| 白玉县| 蕲春县| 新巴尔虎右旗| 丹棱县| 于都县| 东丰县| 云南省| 阿拉善盟| 巴彦淖尔市| 石河子市| 邵阳市| 怀仁县| 南部县| 镇巴县| 长子县| 开封市| 岳阳县| 柳州市| 会泽县| 葵青区| 黄浦区| 宜丰县| 德保县| 鹿邑县| 年辖:市辖区| 集贤县| 孙吴县| 东光县| 民勤县| 桃园市|