java 彻底理解 byte char short int float long double
2017-11-14 10:21
519 查看
遇到过很多关于 数值类型范围的问题了,在这做一个总结,我们可以从多方面理解不同数值类型的所能表示的数值范围
在这里我们只谈论 java中的数值类型
首先说byte:
这段是摘自jdk中 Byte.java中的源代码:
[java] view plaincopy
/**
* 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;
从这里可以看出 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的位数。
再有还可以通过
[java] view plaincopy
System.out.println(Byte.MAX_VALUE); //最大值
System.out.println(Byte.MIN_VALUE); //最小值
输出Byte的最大值和最小值。
综上所述 byte的取值范围只能是:-128 -- 127了 即 负的2的7次方到2的7次方减去1。
相应的 short 作为16位有符号整形,int作为32位有符号整形, long 作为64位有符号整形 都可以如上计算出 取值范围
char作为16位无符号整形 其范围为 0 -- 2的15次方 这无可争议
摘自 Character.java中的源代码:
[java] view plaincopy
/**
* 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';
float作为32位的浮点型:
摘自Float.java源码:
[java] view plaincopy
/**
* 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源码:
[c-sharp] view plaincopy
/**
* A constant holding the largest positive finite value of type
* <code>double</code>,
* (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to
* the hexadecimal floating-point literal
* <code>0x1.fffffffffffffP+1023</code> and also equal to
* <code>Double.longBitsToDouble(0x7fefffffffffffffL)</code>.
*/
public static final double MAX_VALUE = 1.7976931348623157e+308; // 0x1.fffffffffffffP+1023
/**
* A constant holding the smallest positive nonzero value of type
* <code>double</code>, 2<sup>-1074</sup>. It is equal to the
* hexadecimal floating-point literal
* <code>0x0.0000000000001P-1022</code> and also equal to
* <code>Double.longBitsToDouble(0x1L)</code>.
*/
public static final double MIN_VALUE = 4.9e-324; // 0x0.0000000000001P-1022
在这里我们只谈论 java中的数值类型
首先说byte:
这段是摘自jdk中 Byte.java中的源代码:
[java] view plaincopy
/**
* 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;
从这里可以看出 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的位数。
再有还可以通过
[java] view plaincopy
System.out.println(Byte.MAX_VALUE); //最大值
System.out.println(Byte.MIN_VALUE); //最小值
输出Byte的最大值和最小值。
综上所述 byte的取值范围只能是:-128 -- 127了 即 负的2的7次方到2的7次方减去1。
相应的 short 作为16位有符号整形,int作为32位有符号整形, long 作为64位有符号整形 都可以如上计算出 取值范围
char作为16位无符号整形 其范围为 0 -- 2的15次方 这无可争议
摘自 Character.java中的源代码:
[java] view plaincopy
/**
* 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';
float作为32位的浮点型:
摘自Float.java源码:
[java] view plaincopy
/**
* 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源码:
[c-sharp] view plaincopy
/**
* A constant holding the largest positive finite value of type
* <code>double</code>,
* (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to
* the hexadecimal floating-point literal
* <code>0x1.fffffffffffffP+1023</code> and also equal to
* <code>Double.longBitsToDouble(0x7fefffffffffffffL)</code>.
*/
public static final double MAX_VALUE = 1.7976931348623157e+308; // 0x1.fffffffffffffP+1023
/**
* A constant holding the smallest positive nonzero value of type
* <code>double</code>, 2<sup>-1074</sup>. It is equal to the
* hexadecimal floating-point literal
* <code>0x0.0000000000001P-1022</code> and also equal to
* <code>Double.longBitsToDouble(0x1L)</code>.
*/
public static final double MIN_VALUE = 4.9e-324; // 0x0.0000000000001P-1022
相关文章推荐
- java 彻底理解 byte char short int float long double
- 【转】java 彻底理解 byte char short int float long double
- java 彻底理解 byte char short int float long double (转)
- java 彻底理解 byte char short int float long double
- java 彻底理解 byte char short int float long double
- java 彻底理解 byte char short int float long double
- java 彻底理解 byte char short int float long double
- java 彻底理解 byte char short int float long double
- Java中byte数组与short,int,char,long,float,double之间的转换
- java:byte short int long float double char
- Java基础-数据类型int,short,char,long,float,double,boolean,byte
- java 基本数值类型:byte short int long float double char
- Java中byte数组与short,int,char,long,float,double之间的转换
- java byte、short、int、long、float、double、char基本数据类型范围
- C 语言包含的数据类型如下图所示: 一、数据类型与“模子” short、int、long、char、float、double 这六个关键字代表C 语言里的六种基本数据类型。 怎么去理解它们呢? 举个
- 关键字short、int、long、char、float、double、signed、unsigned的理解
- java中byte、short、int、long、float、double类型的取值范围
- java Byte和各数据类型(short,int,long,float,double)之间的转换
- java的int、char、long、float、double对byte的转换
- java Byte和各数据类型(short,int,long,float,double)之间的转换