在java中编写控制台输出

时间:2016-07-28 17:52:53

标签: java console output

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

以下行如何生成 a 作为输出。

System.out.write(1889);

2 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:2)

根据this answer System.out.write(int)以系统相关的方式将最低有效字节写入输出。在您的情况下,系统决定将其写为字符。

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

这两个数字的最右边的八位字节是相同的。正如@Cricket所提到的,这基本上与你传入的数字的模数和256相同。