2017-09-26 50 views

回答

0

短值仅限于-32768至+32767,而Java在投射至短时只保留最低有效16位(a narrowing primitive conversion, JLS 5.1.3)。

高16位将丢失

例5.1.3-2。缩小丢失信息的原始转换

class Test { 
    public static void main(String[] args) { 
     // A narrowing of int to short loses high bits: 
     System.out.println("(short)0x12345678==0x" + 
          Integer.toHexString((short)0x12345678)); 
     // An int value too big for byte changes sign and magnitude: 
     System.out.println("(byte)255==" + (byte)255); 
     // A float value too big to fit gives largest int value: 
     System.out.println("(int)1e20f==" + (int)1e20f); 
     // A NaN converted to int yields zero: 
     System.out.println("(int)NaN==" + (int)Float.NaN); 
     // A double value too large for float yields infinity: 
     System.out.println("(float)-1e100==" + (float)-1e100); 
     // A double value too small for float underflows to zero: 
     System.out.println("(float)1e-50==" + (float)1e-50); 
    } 
} 
相关问题