您的位置:首页 > 编程语言 > Java开发

java中byte、short、int、long、float、double类型的取值范围

2015-03-20 16:57 786 查看
首先byte、short、int、long都是整数类型。

     ①:byte占一个字节,也就是8位,而且byte 是有符号整形 用二进制表示时候最高位为符号位 0代表正数 1代表负数。

max:01111111(十进制:2^8-1=127)    

min:是11111111吗?答案不是的。8位总共能表示256个数。00000000表示0,那10000000表示?

要注意正数在计算机中是以原码形式存在的,负数在计算机中是以其补码形式存在的,那么一个负数的补码是怎么计算的呢?
就是负数的绝对值的原码转为二进制再按位取反后加1。

-128的绝对值128,128源码10000000
取反:01111111 加1:10000000 故-128计算机中的表示就是 1000 0000 了。

-2^7(-128)<byte<2^7-1(127)
jdk中
Byte.class中的源代码:

 /**
     * A constant holding the minimum value a <code>byte</code> can
     * have, -2<sup>7</sup>.
     */
    public static final byte   MIN_VALUE = -128;

    /**

     * A constant holding the maximum value a <code>byte</code> can

     * have, 2<sup>7</sup>-1.

     */

    public static final byte   MAX_VALUE = 127;
     ②同理:short占2位,int占4位,long占8位,都是有符号整型。
 /**

     * A constant holding the minimum value a <code>short</code> can

     * have, -2<sup>15</sup>.

     */

    public static final short   MIN_VALUE = -32768;

    /**

     * A constant holding the maximum value a <code>short</code> can

     * have, 2<sup>15</sup>-1.

     */

    public static final short   MAX_VALUE = 32767;

                                     -2^15(-32768)<byte<2^15-1(32767)

/**

     * A constant holding the minimum value an <code>int</code> can

     * have, -2<sup>31</sup>.

     */

    public static final int   MIN_VALUE = 0x80000000;

    /**

     * A constant holding the maximum value an <code>int</code> can

     * have, 2<sup>31</sup>-1.

     */

    public static final int   MAX_VALUE = 0x7fffffff;

 
                          -2^31(-2147483648)<byte<2^31-1(2147483647)

 /**

     * A constant holding the minimum value a <code>long</code> can

     * have, -2<sup>63</sup>.

     */

    public static final long MIN_VALUE = 0x8000000000000000L;

    /**

     * A constant holding the maximum value a <code>long</code> can

     * have, 2<sup>63</sup>-1.

     */

    public static final long MAX_VALUE = 0x7fffffffffffffffL;

   
                   
4000
               -2^63<byte<2^63-1
char作为16位无符号整形
其范围为 0 —— 2^15,float占4位,double占8位。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 编程