你有一个表示错误的问题。当您有溢出时,这一点更加明显。
long l = 1234567890123456789L;
double d = l;
float f = l;
int i = (int) l;
short s = (short) l;
char ch = (char) l;
byte b = (byte) l;
System.out.println("l= " + l + " in hex " + Long.toHexString(l));
System.out.println("d= " + d);
System.out.println("f= " + f);
System.out.println("i= " + i + " in hex " + Integer.toHexString(i));
System.out.println("s= " + s + " in hex " + Integer.toHexString(s & 0xFFFF));
System.out.println("(int) ch= " + (int) ch + " in hex " + Integer.toHexString(ch));
System.out.println("b= " + b + " in hex " + Integer.toHexString(b));
印刷
l= 1234567890123456789 in hex 112210f47de98115
d= 1.23456789012345677E18
f= 1.23456794E18
i= 2112454933 in hex 7de98115
s= -32491 in hex 8115
(int) ch= 33045 in hex 8115
b= 21 in hex 15
只能long
无错误地表示这个值(加上 BigInteger 和 BigDecimal)所有其他数据类型都有不同的错误。float
和double
准确地表示最高位,而、int
和准确地表示最低位。short
char
byte