Writing to the console 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');
}
}

output of this program

A
a
a

What the next line in line a looks like .

System.out.write(1889);
+4
source share
2 answers

Because 1889 % 256 = 97. There are 256 ASCII characters, so it is mod operatorused to get a valid character.

+9
source

In accordance with this answer System.out.write(int) , the least significant byte is written to the output in a system-dependent manner. In your case, the system decided to write it as a symbol.

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

The rightmost octet is the same for both numbers. As @Cricket mentions, this is essentially the same as the module module in which you go in and 256.

+2
source

Source: https://habr.com/ru/post/1649450/


All Articles