java基本数据类型转换

java的基本数据类型

数据类型位数(B)
byte 1
char2
short2
int4
long8
float4
double8

注:各种基本数据类型除了char以外,都属于数值类型,java中所有的数值类型默认数是符号的;以下讨论中的数值类型都是有符号的
下面是各种数据类型的有效范围,来自jdk
byte       MAX_VALUE = 2^7-1 MIN_VALUE = -2^7 

 */
public final class Byte extends Number implements Comparable<Byte> {

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

    /**
     * A constant holding the maximum value a {@code byte} can
     * have, 2<sup>7</sup>-1.
     */
    public static final byte   MAX_VALUE = 127;

    /**
     * The {@code Class} instance representing the primitive type
     * {@code byte}.
同理
short MAX_VALUE = 2^15 -1 MIN_VALUE = -2^15  
int    MAX_VALUE = 2^31 -1 MIN_VALUE = -2^31  
long MAX_VALUE = 2^63 -1 MIN_VALUE = -2^63

对于float和double,相对复杂,浮点数表示方式是IEEE标准
float MAX_VALUE = (2-2^-23)*2^127 MIN_VALUE = 2^-23*2^-126
public final class Float extends Number implements Comparable<Float> {
    /**
     * A constant holding the positive infinity of type
     * {@code float}. It is equal to the value returned by
     * {@code Float.intBitsToFloat(0x7f800000)}.
     */
    public static final float POSITIVE_INFINITY = 1.0f / 0.0f;

    /**
     * A constant holding the negative infinity of type
     * {@code float}. It is equal to the value returned by
     * {@code Float.intBitsToFloat(0xff800000)}.
     */
    public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;

    /**
     * A constant holding a Not-a-Number (NaN) value of type
     * {@code float}.  It is equivalent to the value returned by
     * {@code Float.intBitsToFloat(0x7fc00000)}.
     */
    public static final float NaN = 0.0f / 0.0f;

    /**
     * A constant holding the largest positive finite value of type
     * {@code float}, (2-2<sup>-23</sup>)·2<sup>127</sup>.
     * It is equal to the hexadecimal floating-point literal
     * {@code 0x1.fffffeP+127f} and also equal to
     * {@code Float.intBitsToFloat(0x7f7fffff)}.
     */
    public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f

    /**
     * A constant holding the smallest positive normal value of type
     * {@code float}, 2<sup>-126</sup>.  It is equal to the
     * hexadecimal floating-point literal {@code 0x1.0p-126f} and also
     * equal to {@code Float.intBitsToFloat(0x00800000)}.
     *
     * @since 1.6
     */
    public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f

    /**
     * A constant holding the smallest positive nonzero value of type
     * {@code float}, 2<sup>-149</sup>. It is equal to the
     * hexadecimal floating-point literal {@code 0x0.000002P-126f}
     * and also equal to {@code Float.intBitsToFloat(0x1)}.
     */
    public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f

    /**
     * Maximum exponent a finite {@code float} variable may have.  It
     * is equal to the value returned by {@code
     * Math.getExponent(Float.MAX_VALUE)}.
     *
     * @since 1.6
     */
    public static final int MAX_EXPONENT = 127;

    /**
     * Minimum exponent a normalized {@code float} variable may have.
     * It is equal to the value returned by {@code
     * Math.getExponent(Float.MIN_NORMAL)}.
     *
     * @since 1.6
     */
    public static final int MIN_EXPONENT = -126;

    /**
     * The number of bits used to represent a {@code float} value.
     *
     * @since 1.5
     */
    public static final int SIZE = 32;

    /**
     * The {@code Class} instance representing the primitive type
     * {@code float}.
     *
     * @since JDK1.1
     */
注意:从源码里可以看出,这里MAX_VALUE和MIN_VALUE并不是正负最大最小,而是正的( A constant holding the largest positive finite value of type)最大最小;为什么不是正负呢?这里必须要弄清楚浮点数的表示格式(我会另外写一篇关于计算机数据表示和处理的文章),个人认为也没有必要写出负数,因为浮点数和整数类型不一样,浮点数表示是对称的,而整数是不对称的。所以jdk认为没有必要给出负的,很明显float的最小负数(这里是指能打印出来的)是-MAX_VALUE

double MAX_VALUE = (2-2^-52)*2^1023 MIN_VALUE = 2^-52*2^-1022
public final class Double extends Number implements Comparable<Double> {
    /**
     * A constant holding the positive infinity of type
     * {@code double}. It is equal to the value returned by
     * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
     */
    public static final double POSITIVE_INFINITY = 1.0 / 0.0;

    /**
     * A constant holding the negative infinity of type
     * {@code double}. It is equal to the value returned by
     * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
     */
    public static final double NEGATIVE_INFINITY = -1.0 / 0.0;

    /**
     * A constant holding a Not-a-Number (NaN) value of type
     * {@code double}. It is equivalent to the value returned by
     * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
     */
    public static final double NaN = 0.0d / 0.0;

    /**
     * A constant holding the largest positive finite value of type
     * {@code double},
     * (2-2<sup>-52</sup>)·2<sup>1023</sup>.  It is equal to
     * the hexadecimal floating-point literal
     * {@code 0x1.fffffffffffffP+1023} and also equal to
     * {@code Double.longBitsToDouble(0x7fefffffffffffffL)}.
     */
    public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308

    /**
     * A constant holding the smallest positive normal value of type
     * {@code double}, 2<sup>-1022</sup>.  It is equal to the
     * hexadecimal floating-point literal {@code 0x1.0p-1022} and also
     * equal to {@code Double.longBitsToDouble(0x0010000000000000L)}.
     *
     * @since 1.6
     */
    public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308

    /**
     * A constant holding the smallest positive nonzero value of type
     * {@code double}, 2<sup>-1074</sup>. It is equal to the
     * hexadecimal floating-point literal
     * {@code 0x0.0000000000001P-1022} and also equal to
     * {@code Double.longBitsToDouble(0x1L)}.
     */
    public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324

    /**
     * Maximum exponent a finite {@code double} variable may have.
     * It is equal to the value returned by
     * {@code Math.getExponent(Double.MAX_VALUE)}.
     *
     * @since 1.6
     */
    public static final int MAX_EXPONENT = 1023;

    /**
     * Minimum exponent a normalized {@code double} variable may
     * have.  It is equal to the value returned by
     * {@code Math.getExponent(Double.MIN_NORMAL)}.
     *
     * @since 1.6
     */
    public static final int MIN_EXPONENT = -1022;

    /**
     * The number of bits used to represent a {@code double} value.
     *
     * @since 1.5
     */
    public static final int SIZE = 64;
再看看java中直接打印出这些值的情况,下面是一个测试代码
public class NumberLimit{
	public static void main(String arg[]){
		System.out.println("char MAX_VALUE "+Character. MAX_VALUE);//char属于非数值类型故是无符号的,最大0xffff,Unicode码是?
		System.out.println("char MIN_VALUE "+Character. MIN_VALUE);//最小0x0000
		
		System.out.println("byte MAX_VALUE "+Byte.MAX_VALUE);//2^7-1
		System.out.println("byte MIN_VALUE "+Byte. MIN_VALUE);//-2^7
		
		System.out.println("short MAX_VALUE "+Short.MAX_VALUE);//2^15-1
		System.out.println("short MIN_VALUE "+Short.MIN_VALUE);//-2^15
		System.out.println("int MAX_VALUE "+Integer.MAX_VALUE);//2^31-1
		System.out.println("int MIN_VALUE "+Integer.MIN_VALUE);//-2^31
		System.out.println("long MAX_VALUE "+Long.MAX_VALUE);//2^63-1
		System.out.println("long MIN_VALUE "+Long.MIN_VALUE);//-2^63
		
		System.out.println("float MAX_VALUE "+Float.MAX_VALUE);//(2-2^-23)*2^127
		System.out.println("float MIN_VALUE "+Float.MIN_VALUE);//2^-23*2^-126
		System.out.println("double MAX_VALUE "+Double.MAX_VALUE);//(2-2^-52)*2^1023
		System.out.println("double MIN_VALUE "+Double.MIN_VALUE);//2^-52*2^-1022
		//float ff = 3.4028236E38f;//浮点数过大
		float ff = -3.4028235E38f;//
		double dd = 1.7976931348623158E308;//不报错???但是比1.7976931348623157E308大一点
											//1.7976931348623159E308才开始报错
		System.out.println(ff);
		System.out.println(dd);//打印出来的dd是1.7976931348623157E308
	}
}

注:这里写的数值范围是不能超过定义的范围的,不过对于double类型的那个,个人认为是计算机精度的误差使得 1.7976931348623158E308也不报错,可能是java编译器把他当做了 1.7976931348623157E308处理


java基本数据类型的相互转换
1.隐式转换:
系统运算过程中,容量小的向容量大的转换
byte char short ->int ->long ->float ->double
byte char short 之间不会互相转换,他们三者之间运算时首先会转换为int类型
声明过程中整数默认为int,小数默认为double


2.强制转换
容量大的向容量小的转换时,必须加强制转换符,且此时可能会产生溢出或者精度的降低

对于long ->int ,直接砍掉四个字节即可(因此,如果long本身的表示并未超过int的范围,并不会出错;但是long本身如果超过int范围的话,就会出错)
对于double ->float,由于表示方式和整型不一样,并不是截掉一部分,所以此过程很容易产生溢出或者精度降低


3.后缀表示
声明float 和long时要注意加f和l
且float f=1.23f和float f=(float)1.23并不一样,前者在计算机中就存了个float类型的1.23,但是后者实际上由double转换过来的

下面是一个测试代码
public class TestConvert{
	public static void main(String arg[]){
		int i1=123;
		int i2=456;
		double d1=(i1+i2)*1.2; //系统将(i1+i2)转换为double型运算
		float f1=(float)((i1+i2)*1.2);//需要加强制转换符,可能会溢出
		byte b1=67;
		byte b2=89;
		byte b3=(byte)(b1+b2);//系统将(b1+b2)转换为int型运算,故需要强制转换
		System.out.println(b3);
		
		double d2=1e200;
		float f2=(float)d2;//产生溢出
		System.out.println(f2);
		
		//float f3=1.23;//编译错误
		float f3=1.23f;//1.23 默认为double,故加上f后缀
		long l1=123;
		long l2=300000000000L;//300000000000超出了int的范围,故要加l后缀
		float f=l1*l2*f3;//系统将转换为float型运算
		long l=(long)f;//强制转换会舍去小数部分(不是四舍五入),降低精度
	}
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值