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

首先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)

jdk中 Byte.class中的源代码:

 /**
     * A constant holding the minimum value a byte can
     * have, -27.
     */
    public static final byte   MIN_VALUE = -128;
    /**
     * A constant holding the maximum value a byte can
     * have, 27-1.
     */
    public static final byte   MAX_VALUE = 127;
     ②同理:short占2位,int占4位,long占8位,都是有符号整型。
 /**
     * A constant holding the minimum value a short can
     * have, -215.
     */
    public static final short   MIN_VALUE = -32768;


    /**
     * A constant holding the maximum value a short can
     * have, 215-1.
     */
    public static final short   MAX_VALUE = 32767;
                                     -2^15(-32768)
/**
     * A constant holding the minimum value an int can
     * have, -231.
     */
    public static final int   MIN_VALUE = 0x80000000;


    /**
     * A constant holding the maximum value an int can
     * have, 231-1.
     */
    public static final int   MAX_VALUE = 0x7fffffff;

                            -2^31(-2147483648)
 /**
     * A constant holding the minimum value a long can
     * have, -263.
     */
    public static final long MIN_VALUE = 0x8000000000000000L;


    /**
     * A constant holding the maximum value a long can
     * have, 263-1.
     */
    public static final long MAX_VALUE = 0x7fffffffffffffffL;

                                       -2^63
char作为16位无符号整形 其范围为 0 —— 2^15,float占4位,double占8位。


你可能感兴趣的:(java基础)