A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 18334705181 高级黑马   /  2014-9-28 12:33  /  3561 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

什么时候用int,什么时候用double ,什么时候用char?什么时候用short ,什么时候用long?纠结啊,傻傻分不清,求指教。

4 个回复

倒序浏览
首先说一下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的位数。

综上所述 byte的取值范围只能是:-128 -- 127了  即 负的2的7次方到2的7次方减去1。

相应的 short 作为16位有符号整形(短整数,二个字节) int作为32位有符号整形 (整数,四个字节)  long 作为64位有符号整形 (长整数,八个字节) 都可以如上计算出 取值范围。


char作为16位无符号整形 其范围为 0 -- 2的15次方 这无可争议。摘自 Character.java中的源代码:

  1. /**
  2.      * The constant value of this field is the smallest value of type
  3.      * <code>char</code>, <code>'/u0000'</code>.
  4.      *
  5.      * @since   1.0.2
  6.      */  
  7.     public static final char   MIN_VALUE = '/u0000';  
  8.   
  9.     /**
  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';  
复制代码

float作为32位的浮点型   小数 4个字节    摘自Float.java源码:

  1. /**
  2.      * A constant holding the largest positive finite value of type
  3.      * <code>float</code>, (2-2<sup>-23</sup>)·2<sup>127</sup>.
  4.      * It is equal to the hexadecimal floating-point literal
  5.      * <code>0x1.fffffeP+127f</code> and also equal to
  6.      * <code>Float.intBitsToFloat(0x7f7fffff)</code>.
  7.      */  
  8.     public static final float MAX_VALUE = 3.4028235e+38f; // 0x1.fffffeP+127f  
  9.   
  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  
复制代码

double 作为64为浮点型 (双精度浮点, 小数, 8字节)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  
复制代码



回复 使用道具 举报
看你的实际需要 存的是什么数据
回复 使用道具 举报
基本数据类型中有;数值类型(整数与小数点(浮点)类型),字符型也就是(char),比如‘+’,‘-’,‘a’。还有boolean布尔型(这种只有 true or false)。   你要知道,定义一个变量就要在内存中开辟一个空间,而且这个空间要足够能装的下你想要的东西。就涉及到了byte,short,int,long,float,double,char的开辟空间的大小。开辟空间太大,而你要的数据太小,就浪费了内存空间。你往下学习就会熟练了。只要记住默认类型int double。要强转的时候强转就好了。
回复 使用道具 举报
酱料用小碟,米饭用中碗,炖汤用砂锅。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马