你有一個表示錯誤的問題。當你有溢出時,這更明顯。
long l = 123456789L;
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= 123456789in hex 112210f47de98115
d= 1.23456789E18
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
準確地代表最低位。
因爲'float'和'double'不一樣。 ;) – 2012-03-07 11:17:58