2016-07-28 136 views
4
class HelloWorld { 
public static void main(String args[]) { 
int b; 
b = 'A'; 
System.out.write(b); 
System.out.write('\n'); 
System.out.write(97); 
System.out.write('\n'); 
System.out.write(1889); 
System.out.write('\n'); 
} 
} 

这个程序的输出是写作控制台输出

A 
a 
a 

如何以下行产生一个作为输出。

System.out.write(1889); 

回答

9

因为1889 % 256 = 97。有256个ASCII字符,因此mod operator用于获取有效字符。

2

根据this answerSystem.out.write(int)以系统相关的方式将最不重要的字节写入输出。在你的情况下,系统决定把它写成一个字符。

1889 == 0000 0111 0110 0001 
    97 == 0000 0000 0110 0001 

最右边的八位字节对于这两个数字都是相同的。正如@Cricket所提到的,这与获取传入数字的模数和256相同。