简体   繁体   中英

Why does FileInputStream.read() return int?

  1. Why does FileInputStream.read() method return type is int and not byte, clearly when it can read one byte at a time?

  2. Because read() with array allows byte array and not int[] array.

Then why there is a difference here between 1st and 2nd methods?

Proof (why 1st method return type could have been byte):

-> I have a file where it had only one character (whose decimal = 157 and binary = 10011101)

-> When I read the first character using 1st method FileInputStream.read()
which returned me 157 
When type casted to byte, I got -99 which is now represented in 2's 
complement (Here decimal = -99 and binary = 10011101 again). 

Here, we are not losing any binary representation, then what's the real reason behind this?

  1. Why does FileInputStream.read() method return type is int and not byte, clearly when it can read one byte at a time?

  2. Because read() with array allows byte array and not int[] array.

Then why there is a difference here between 1st and 2nd methods?

Proof (why 1st method return type could have been byte):

-> I have a file where it had only one character (whose decimal = 157 and binary = 10011101)

-> When I read the first character using 1st method FileInputStream.read()
which returned me 157 
When type casted to byte, I got -99 which is now represented in 2's 
complement (Here decimal = -99 and binary = 10011101 again). 

Here, we are not losing any binary representation, then what's the real reason behind this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM