石頭JAVA擺地攤兒

          JAVA所有的討論-要一點一點的進步

            BlogJava :: 首頁 ::  :: 聯系 ::  :: 管理 ::
            30 隨筆 :: 0 文章 :: 11 評論 :: 0 Trackbacks
          由于JAVA里沒有sizeof函數,所以想辦法解決一下。

          感謝CSDN網友:dogod

          import java.io.*;

          public class abc {

              
          public static void main(String[] args) {

                  
          char cr = 1;
                  
          byte be = 1;
                  
          short st = 1;
                  
          int it = -1;
                  
          long lg = 1;
                  
          float ft = 1;
                  
          double de = 1;

                  
          int cnt;
                  
          byte x;
                  
          int cot = 0;
                  
          int byteSize = 0;

                  
          // System.out.println("Float.size: "+Float.SIZE);
                  
          // System.out.println("Double.size: "+Double.SIZE);

                  
          for (cnt = 1; (cr <<= 1!= 0; cnt++)
                      ;
                  System.out.println(
          "char'sBits:" + cnt);

                  
          for (cnt = 1; (be <<= 1!= 0; cnt++)
                      ;
                  System.out.println(
          "byte'sBits:" + cnt);
                  byteSize 
          = cnt;

                  
          for (cnt = 1; (st <<= 1!= 0; cnt++)
                      ;
                  System.out.println(
          "short'sBits:" + cnt);

                  
          for (cnt = 1; (it <<= 1!= 0; cnt++)
                      ;
                  System.out.println(
          "int'sBits:" + cnt);

                  
          for (cnt = 1; (lg <<= 1!= 0; cnt++)
                      ;
                  System.out.println(
          "long'sBits:" + cnt);

                  
          // 用字節流處理double
                  try {
                      ByteArrayOutputStream buf 
          = new ByteArrayOutputStream();
                      DataOutputStream out 
          = new DataOutputStream(buf);
                      out.writeDouble(de);
                      out.close();
                      DataInputStream in 
          = new DataInputStream(new ByteArrayInputStream(
                              buf.toByteArray()));

                      
          while ((x = (byte) in.readByte()) != -1{

                          
          // System.out.println("byte["+cot+"]:"+x);
                          cot++;
                      }

                      in.close();

                  }
           catch (IOException e) {
                      System.out.println(
          "EOF!!!");
                  }

                  System.out.println(
          "The Double's bit size is :" + cot * byteSize);

                  
          // 用字節流處理float
                  try {
                      cot 
          = 0;
                      ByteArrayOutputStream buf 
          = new ByteArrayOutputStream();
                      DataOutputStream out 
          = new DataOutputStream(buf);
                      out.writeFloat(ft);
                      out.close();
                      DataInputStream in 
          = new DataInputStream(new ByteArrayInputStream(
                              buf.toByteArray()));

                      
          while ((x = (byte) in.readByte()) != -1{

                          
          // System.out.println("byte["+cot+"]:"+x);
                          cot++;
                      }

                      in.close();

                  }
           catch (IOException e) {
                      System.out.println(
          "EOF!!!");
                  }

                  System.out.println(
          "The Float's bit size is :" + cot * byteSize);

              }


          }

          posted on 2006-03-13 13:33 石頭JAVA擺地攤兒 閱讀(741) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 滦平县| 娱乐| 彰武县| 图木舒克市| 定远县| 彭阳县| 安吉县| 奉新县| 九寨沟县| 佛坪县| 白河县| 彭阳县| 扎赉特旗| 长治县| 无极县| 金平| 乐山市| 甘肃省| 萍乡市| 久治县| 江北区| 洪泽县| 清镇市| 五河县| 弥渡县| 塔河县| 成都市| 雷山县| 龙井市| 蓝田县| 深州市| 山丹县| 五原县| 杨浦区| 正阳县| 炎陵县| 宣汉县| 类乌齐县| 石家庄市| 体育| 龙里县|