遇到过很多关于 数值类型范围的问题了,在这做一个总结,我们可以从多方面理解不同数值类型的所能表示的数值范围
在这里我们只谈论 java中的数值类型
首先说byte:
这段是摘自jdk中 Byte.java中的源代码:
view plaincopy to clipboardprint?
01./**
02. * A constant holding the minimum value a <code>byte</code> can
03. * have, -2<sup>7</sup>.
04. */
05. public static final byte MIN_VALUE = -128;
06.
07. /**
08. * A constant holding the maximum value a <code>byte</code> can
09. * have, 2<sup>7</sup>-1.
10. */
11. public static final byte MAX_VALUE = 127;
/**
* 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的位数。
再有还可以通过
view plaincopy to clipboardprint?
01.System.out.println(Byte.MAX_VALUE); //最大值
02. System.out.println(Byte.MIN_VALUE); //最小值
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中的源代码:
view plaincopy to clipboardprint?
01./**
02. * The constant value of this field is the smallest value of type
03. * <code>char</code>, <code>'\u0000'</code>.
04. *
05. * @since 1.0.2
06. */
07. public static final char MIN_VALUE = '\u0000';
08.
09. /**
10. * The constant value of this field is the largest value of type
11. * <code>char</code>, <code>'\uFFFF'</code>.
12. *
13. * @since 1.0.2
14. */
15. public static final char MAX_VALUE = '\uffff';
/**
* 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源码:
view plaincopy to clipboardprint?
01./**
02. * A constant holding the largest positive finite value of type
03. * <code>float</code>, (2-2<sup>-23</sup>)·2<sup>127</sup>.
04. * It is equal to the hexadecimal floating-point literal
05. * <code>0x1.fffffeP+127f</code> and also equal to
06. * <code>Float.intBitsToFloat(0x7f7fffff)</code>.
07. */
08. public static final float MAX_VALUE = 3.4028235e+38f; // 0x1.fffffeP+127f
09.
10. /**
11. * A constant holding the smallest positive nonzero value of type
12. * <code>float</code>, 2<sup>-149</sup>. It is equal to the
13. * hexadecimal floating-point literal <code>0x0.000002P-126f</code>
14. * and also equal to <code>Float.intBitsToFloat(0x1)</code>.
15. */
16. public static final float MIN_VALUE = 1.4e-45f; // 0x0.000002P-126f
/**
* 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源码:
view plaincopy to clipboardprint?
01./**
02. * A constant holding the largest positive finite value of type
03. * <code>double</code>,
04. * (2-2<sup>-52</sup>)·2<sup>1023</sup>. It is equal to
05. * the hexadecimal floating-point literal
06. * <code>0x1.fffffffffffffP+1023</code> and also equal to
07. * <code>Double.longBitsToDouble(0x7fefffffffffffffL)</code>.
08. */
09. 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
分享到:
相关推荐
char short int long float double 转换成byte数组
举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character
原始类型封装类booleanBoolean charCharacter byteByte shortShort intInteger longLong floatFloat doubleDouble 引用类型和原始类型的行为完全不同,并且它们具有不同的语义。引用类型和原始类型具有不同的特征和...
Int是java的原始数据类型,Integer是java为int提供的封装类。Java为每个原始类型提供了封装类。 原始类型 封装类 boolean Boolean char Character byte Byte short Short int Integer long Long float Float double ...
一、什么是Java Number类? Java Number类是Java中的一个抽象类,它是所有数值类型的超类,包括整数、浮点数和大数。它提供了一组用于操作数值类型的方法,如转换、比较、算术运算等。 二、Java Number类提供了哪些...
7、int和Integer有什么区别? 答:Java是一个近乎纯洁的面向对象...- 原始类型: boolean,char,byte,short,int,long,float,double - 包装类型:Boolean,Character,Byte,Short,Integer,Long,Float,Double
答案: 8种基本类型 char byte short int long float double boolean String和他们的区别你从首字母就能看出来了,String的首字母是大写,因为它是一个类,String和8种基本类型的包装类都属于类基本数据类型...
Java 有8种基本数据类型: byte int short long double float Boolean char byte int short long 都属于整数类型. Double float 属于浮点类型. Boolean 为布尔类型 Char 为字符型 String 不是基本数据类型.它定义的...
返回值的类型 往两者类型大的方向转(byte<short<int<float<long) //特殊情况(含char类型) char和一个byte/short/int 结果为 int char和一个float/double 结果为float/double 二 表达式2和表达式3都是常量: ...
基本数据类型包括byte、int、char、long、float、double、boolean和short。 java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 3、int 和 ...
一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double型之间的转换,整数和String类型之间的转换,以及处理、显示时间方面的问...byte、short、int、long;浮点型float、double。其
2.1java中的8种基本数据类型:boolean byte char short int float double long 2.2:基本数据类型和它对应的封装类型之间可以相互转换,从基本数据类型到封装类 型叫做装箱,从封装类型到基本数据类型叫拆箱,...
键盘输入1个字符串,如果是整数数字串,如”4353”, 分别按八进制和十六进制输出,以及按byte,short,int,long,char输出;每个数占10位长.每行输出3个数; 如果是带小数数字串,如”243.58”,分别按short,int,long,float...
有8种,分别是 boolean、byte、short、int、long、char、float、double
byte→short(char)→int→long→float→double 也就是说byte类型的变量可以自动转换为short类型,示例代码: byte b = 10; short sh = b; 这里在赋值时,JVM首先将b的值转换为short类型,然后再赋值给sh。 在类型...
基本数据类型包括byte、int、char、long、float、double、boolean和short。 java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 3、int 和 ...
原始类型封装类,booleanBoolean,charCharacter,byteByte,shortShort,intInteger,longLong,floatFloat,doubleDouble引用类型和原始类型的行为完全不同,并且它们具有不同的语义。引用类型和原始类型具有不同的特征和...
基本数据类型包括byte、int、char、long、float、double、boolean和short。 java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应 该用StringBuffer类 3、int 和...
byte-Byte short-Short int -Integer long -Long float -Float double -Double char-Character boolean-Boolean 1、所属的包 java.lang 2、类的继承关系、实现接口 前面6个包装类是与数字相关的 父类:Number...
分别是byte, short, int, long, char, float, double, boolean. 1.byte byte属于Java中的整型,长度为1字节8bit,取值10000000(-128)到 01111111(127),变量初始化默认值为0,包装类Byte 2.short short属于Java...