Java中基本數據類型byte,short,char,int,long,float,double 取值范圍


部分內容轉自:java 徹底理解 byte char short int float long double

 首先說byte:

這段是摘自jdk中 Byte.java中的源代碼:

 

 1 /**
 2      * A constant holding the minimum value a <code>byte</code> can
 3      * have, -2<sup>7</sup>.
 4      */
 5     public static final byte   MIN_VALUE = -128;
 6 
 7     /**
 8      * A constant holding the maximum value a <code>byte</code> can
 9      * have, 2<sup>7</sup>-1.
10      */
11     public static final byte   MAX_VALUE = 127;

 

 

從這里可以看出 byte的取值范圍:-128 --- 127;

從計算機組成原理的角度可以解釋:byte在計算機中是占8個字節的 而且byte 是有符號整形 用二進制表示時候最高位為符號位 0代表正數 1代表負數。

最大值:127      0111 1111 即2的7次方減去1;

最小值:-128 這個數字曾經困擾我很久, 要知道正數在計算機中是以原碼形式存在的,負數在計算機中是以其補碼形式存在的,那么一個負數的補碼是怎么計算的呢? 就是負數的絕對值的原碼轉為二進制再按位取反后加1,

下邊這個10和-10為例來介紹的 :10原碼:0000 1010   它在計算機中的存儲就是 0000 1010, 那么-10呢? 按照前面說的 算除其絕對值為10,轉為二進制 0000 1010 按位取反 1111 0101 再加1后:1111 0110,此為-10補碼 ,好的,計算機中的1111 0110就是代表-10了。

 我們來看 -128  絕對值128的二進制表示:1000 0000 按位取反 0111 1111 加1后:1000 0000,也就是說 -128在計算機中的表示就是 1000 0000 了, 再來看一下-129 在計算機中的表示,絕對值129的范圍已經超出了了byte的位數。

再有還可以通過

 

1 System.out.println(Byte.MAX_VALUE); //最大值
2 System.out.println(Byte.MIN_VALUE); //最小值

 

 

輸出Byte的最大值和最小值。

綜上所述 byte的取值范圍只能是:-128 -- 127了  即 負的2的7次方到2的7次方減去1。

相應的 short 作為16位有符號整形,int作為32位有符號整形,  long 作為64位有符號整形 都可以如上計算出 取值范圍

 

Short:

 

 

Unicode Character Representations

The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value.

   Character.java中的源代碼:

 

/**
     * The constant value of this field is the smallest value of type
     * <code>char</code>, <code>'/u0000'</code>.
     *
     * @since   1.0.2
     */
    public static final char   MIN_VALUE = '/u0000';

    /**
     * The constant value of this field is the largest value of type
     * <code>char</code>, <code>'/uFFFF'</code>.
     *
     * @since   1.0.2
     */
    public static final char   MAX_VALUE = '/uffff';

 

 

 

 Long:

 

 

float作為32位的浮點型:

摘自Float.java源碼:

 

/**
     * A constant holding the largest positive finite value of type
     * <code>float</code>, (2-2<sup>-23</sup>)·2<sup>127</sup>.
     * It is equal to the hexadecimal floating-point literal
     * <code>0x1.fffffeP+127f</code> and also equal to
     * <code>Float.intBitsToFloat(0x7f7fffff)</code>.
     */
    public static final float MAX_VALUE = 3.4028235e+38f; // 0x1.fffffeP+127f

    /**
     * A constant holding the smallest positive nonzero value of type
     * <code>float</code>, 2<sup>-149</sup>. It is equal to the
     * hexadecimal floating-point literal <code>0x0.000002P-126f</code>
     * and also equal to <code>Float.intBitsToFloat(0x1)</code>.
     */
    public static final float MIN_VALUE = 1.4e-45f; // 0x0.000002P-126f

 

 

 

double 作為64為浮點型

Double.java源碼:

 

 1 /**
 2      * A constant holding the largest positive finite value of type
 3      * <code>double</code>,
 4      * (2-2<sup>-52</sup>)·2<sup>1023</sup>.  It is equal to
 5      * the hexadecimal floating-point literal
 6      * <code>0x1.fffffffffffffP+1023</code> and also equal to
 7      * <code>Double.longBitsToDouble(0x7fefffffffffffffL)</code>.
 8      */
 9     public static final double MAX_VALUE = 1.7976931348623157e+308; // 0x1.fffffffffffffP+1023
10 
11     /**
12      * A constant holding the smallest positive nonzero value of type
13      * <code>double</code>, 2<sup>-1074</sup>. It is equal to the
14      * hexadecimal floating-point literal
15      * <code>0x0.0000000000001P-1022</code> and also equal to
16      * <code>Double.longBitsToDouble(0x1L)</code>.
17      */
18     public static final double MIN_VALUE = 4.9e-324; // 0x0.0000000000001P-1022

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM