java.io.FileInputStream.read()的返回值类型为什么是int而不是byte
cjapm 2009-11-21 11:32:28 刚在Sun的网站上读到一篇文档,文中提到关于java.io.FileInputStream.read()的返回值类型的问题。那段话是这么写的:
Notice that read() returns an int value. If the input is a stream of bytes, why doesn't read() return a byte value? Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream.
我刚查了一下JDK的文档,byte类型的取值范围是-2^7到2^7-1,说明-1在此范围内,如果仅仅为了返回-1的话完全可以用byte作为返回类型的啊?
请高人指点,谢谢!
原文链接如下:http://java.sun.com/docs/books/tutorial/essential/io/bytestreams.html