为什么InputStream#read()返回一个int而不是一个字节? [英] Why does InputStream#read() return an int and not a byte?

查看:127
本文介绍了为什么InputStream#read()返回一个int而不是一个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 InputStream #read() 返回 int 而不是字节

推荐答案

因为字节只能保留-128直到127,而它应该返回0到255(当没有字节时返回-1(即EOF))。即使它返回 byte ,也没有空间来表示EOF。

Because a byte can only hold -128 until 127, while it should return 0 until 255 (and -1 when there's no byte left (i.e. EOF)). Even if it returned byte, there would be no room to represent EOF.

一个更有趣的问题是为什么它不返回

A more interesting question is why it doesn't return short.

这篇关于为什么InputStream#read()返回一个int而不是一个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆