java基本数据类型转换,java数据类型转换
分享于 点击 10766 次 点评:264
java基本数据类型转换,java数据类型转换
java的基本数据类型数据类型 | 位数(B) |
byte | 1 |
char | 2 |
short | 2 |
int | 4 |
long | 8 |
float | 4 |
double | 8 |
注:各种基本数据类型除了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^127MIN_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_VALUEdouble 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;//强制转换会舍去小数部分(不是四舍五入),降低精度
}
}
相关文章
- 暂无相关文章
用户点评