James Gosling 对 Java 字节为何有符号的解释
最初我很惊讶 Java 决定指定 byte
有符号,范围为 -128..127
(含)。我的印象是大多数 8 位数字表示形式都是无符号的,其范围为 0..255
(例如 点十进制表示法的 IPv4)。
那么 James Gosling 是否曾被要求解释为什么他决定对 byte
进行签名?权威编程语言设计者和/或评论家之间过去是否对这个问题进行过值得注意的讨论/辩论?
I was initially surprised that Java decides to specify that byte
is signed, with a range from -128..127
(inclusive). I'm under the impression that most 8-bit number representations are unsigned, with a range of 0..255
instead (e.g. IPv4 in dot-decimal notation).
So has James Gosling ever been asked to explain why he decided that byte
is signed? Has there been notable discussions/debates about this issue in the past between authoritative programming language designers and/or critics?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来简单是主要原因。来自本次采访:
我最初的假设是,这是因为 Java 根本没有无符号数字类型。为什么
byte
应该是一个例外?char
是一种特殊情况,因为它必须表示 UTF-16 代码单元(感谢 Jon Skeet 的引用)It appears that simplicity was the main reason. From this interview:
My initial assumption was that it's because Java doesn't have unsigned numeric types at all. Why should
byte
be an exception?char
is a special case because it has to represent UTF-16 code units (thanks to Jon Skeet for the quote)根据“Oak 语言规范 0.2”(又名 Java 语言):
“Oak 字节类型是 C 程序员习惯于将其视为 char 类型的类型。但在 Oak 语言中,字符为 16 位宽。具有单独的字节type 消除了 C 语言中 char 解释为 8 位整数和字符之间的混淆。”
您可以从这里获取附记副本:
http://cretesoft.com/archive/files/OakSpec0.2.ps (scribd 上的部分副本)
此外,该网站上还发布了采访的一部分:(他为 java 中缺少无符号字节辩护)
< a href="http://www.darksleep.com/player/JavaAndUnsignedTypes.html" rel="noreferrer">http://www.darksleep.com/player/JavaAndUnsignedTypes.html
添加来自以下内容的采访上述页面...
*”
http://www.gotw.ca/publications/c_family_interview.htm
另一方面....根据 http://www.artima.com /weblogs/viewpost.jsp?thread=7555
As per 'Oak Language Specification 0.2' aka Java language:
"The Oak byte type is what C programmers are used to thinking of as the char type. But in the Oak language, characters are 16 bits wide. Having a separate byte type removes the confusion in C between the interpretation of char as an 8 bit integer and as a character."
You can grab a postscript copy from here :
http://cretesoft.com/archive/files/OakSpec0.2.ps (partial copy on scribd)
Also there is a part of interview posted on this site: (Where he is defending the absence of unsigned byte in java)
http://www.darksleep.com/player/JavaAndUnsignedTypes.html
Adding the interview taken from the above mentioned page...
*"
http://www.gotw.ca/publications/c_family_interview.htm
On the other hand.... According to http://www.artima.com/weblogs/viewpost.jsp?thread=7555
我不知道 James Gosling 有任何直接引用,但有一个关于 unsigned
byte
的官方 RFE:I'm not aware of any direct quotes from James Gosling, but there's an official RFE for unsigned
byte
:字节
没有理由被无符号。当您使用char
类型来表示字符时,byte
通常不会完成char
的工作。There's no reason for a
byte
to be unsigned. when you havechar
type to represent characters, thebyte
would normally not do that job of achar
.