Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why read() method of FileInputStream returns int instead of byte?

 
Suresh Rachuru
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Oracle documentation (http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html) says "Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream."

My doubt is why can't be return type is 'byte' as it can hold value -1 ? Any clarification is appreciated. Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method needs to be able to read a byte of data (256 possible values) or indicate there are no more values. So it needs to return 257 possibilities. A byte can only hold 256. If it returned a byte, how do you tell the difference between reading a -1 from the file and reaching the end of the stream?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic