[英]writing console output in java
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);
因為1889 % 256 = 97
。 有256個ASCII字符,因此mod operator
用於獲取有效字符。
根據這個答案, System.out.write(int)
以系統相關的方式將最低有效字節寫入輸出。 在您的情況下,系統決定將其寫為字符。
1889 == 0000 0111 0110 0001
97 == 0000 0000 0110 0001
兩個數字的最右邊的八位字節是相同的。 正如@Cricket所提到的,這基本上與你傳入的數字的模數和256相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.