posts - 24,  comments - 29,  trackbacks - 0
          基本類型的初始化
                  我們先來看兩個例子,看一下會發生什么問題,得到什么答案。

          /*測試基本類型的初始化*/

          public class InitPrimitive1

             byte a;
             short b;
             int c;
             long d;
             float e;
             double f;
             char g;
             boolean h;

          public static void main(String[] args)
            {
             InitPrimitive1 aInit = new InitPrimitive1();
            aInit.print();
             }
          public void print()
              {
                System.out.println("字節型,a = " + a);
                System.out.println("短整型,b = " + b);
                System.out.println("整數型,c = " + c);
                System.out.println("長整型,d = " + d);
                System.out.println("單精度型,e = " + e);
                System.out.println("雙精度型,f  = " + f );
                System.out.println("字符型,g = " + g);
                System.out.println("布爾型,h = " + h);
               }
          }


          /*測試基本類型初始化*/

          public class InitPrimitive2
          {
           public static void main(String[] args)
            {
             InitPrimitive2 aInit = new InitPrimitive2();
             aInit.print();
             }
          public void print()
             {
              byte a;
              short b;
              int c;
              long d;
              float e;
              double f;
              char g;
              boolean h;
              System.out.println("字節型,a =" + a);
              System.out.println("短整型,b =" + b);
              System.out.println("整數型,c =" + c);
              System.out.println("長整型,d =" + d);
              System.out.println("單精度型,e =" + e);
              System.out.println("雙精度型,f =" + f);
              System.out.println("字符型,g =" + g);
              System.out.println("布爾型,h =" + h);
              }
          }

          我們先看一下這兩段代碼的區別:
                 這八個基本類型的變量聲明位于不同的位置,再就沒有什么區別了。再看一下運行結果。
          InitPrimitive1.java輸出結果:
            字節型,a = 0
            短整型,b = 0
            整數型,c = 0
            長整型,d = 0
            單精度型,e = 0.0
            雙精度型,f = 0.0
            字符型,g =
            布爾型,h = false 

          InitPrimitive2.java輸出結果:
            InitPrimitive2.java:23:variable a might not have been initialized
            InitPrimitive2.java:24:variable b might not have been initialized
            InitPrimitive2.java:25:variable c might not have been initialized
            InitPrimitive2.java:26:variable d might not have been initialized
            InitPrimitive2.java:27:variable e might not have been initialized
            InitPrimitive2.java:28:variable f might not have been initialized
            InitPrimitive2.java:29:variable g might not have been initialized
            InitPrimitive2.java:30:variable h might not have been initialized

          下面來解釋一下。在1當中我們聲明的是全局變量,在2中聲明的是局部變量,可以得到基本類型初始化的規則:
                 基本類型作為全局變量時可以不用初始化,系統會自動初始化,這個自動初始化的值我們稱為默認值,如果作為局部變量時,程序必須初始化,系統不會為局部變量進行初始化工作。
          posted on 2007-05-09 11:30 心隨Wǒ動 閱讀(371) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2007年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          QQ群:9028409
          QQ:29850994
          點擊這里給我發消息

          留言簿(2)

          隨筆檔案

          積分與排名

          • 積分 - 31465
          • 排名 - 1347

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 海淀区| 河北区| 绥宁县| 西盟| 布拖县| 通江县| 定襄县| 通榆县| 宁陵县| 安顺市| 镇沅| 贺兰县| 鞍山市| 泾源县| 措美县| 名山县| 长乐市| 营口市| 习水县| 夏津县| 万盛区| 南陵县| 木里| 荔波县| 淮安市| 靖边县| 武义县| 宜良县| 穆棱市| 宜川县| 淮安市| 鲁山县| 开远市| 东乡族自治县| 察隅县| 元朗区| 姜堰市| 无棣县| 太仓市| 永年县| 巴里|