表現に誤りがある問題。これは、オーバーフローがある場合により明白になります。
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は最下位ビットを正確に表します。