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

编程入门 行业动态 更新时间:2024-10-28 08:19:36
本文介绍了为什么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而不是一个字节?

本文发布于:2023-11-10 09:15:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   而不是   InputStream   read   int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!