What does System.in.read really return?

What is he doing:

System.in.read()

to return? The documentation says:

Returns: the next byte of data or -1 if the end of the stream is reached.

But, for example, if I enter: 10I return 49. Why is this?

+5
source share
3 answers

49is the ASCII char value 1. This is the value of the first byte.

The byte stream that is created when you type 1 0 Enteron your console or terminal contains three bytes {49,48,10}(on my Mac, it may end 10, 12 or 12 instead of 10, depending on your system).

So, the conclusion of a simple fragment

int b = System.in.read();
while (b != -1) {
    System.out.println(b);
    b = System.in.read();
}

after entering 10 and entering input, there is (on my machine)

49
48
10
+13
source

System.in.read() .

49 - Unicode 1.

:

System.out.println((char)49);

.

+5

10 , , , .

49 - ASCII 1.

+3

All Articles