End of stream in JAVA

I am confused by the following statement that appears here

The main read () method of the InputStream class reads one unsigned byte of data and returns an unsigned int byte. This is a number between 0 and 255. If the end of the stream collides, it returns -1; and you can use this as a flag to keep track of the end of the stream.

Since a single byte can represent up to 256 integers, I don't see how it can represent 0 to 256 and -1. Can someone please comment on what I am missing here?

+3


source to share


3 answers


The return type of InputStream # read () is int

where the value can be read as byte

if it falls in the range 0-255.



+3


source


Although the operation read()

just reads a byte, it actually returns int

, so no problem.



Only values ​​in the range 0-255 are returned, though, other than the special end-of-stream value.

+3


source


It returns int

, not byte

, so while it will usually only contain 0-255, it may contain other values.

+3


source







All Articles