James Gosling 对 Java 字节为何有符号的解释

发布于 2024-09-06 12:46:00 字数 332 浏览 3 评论 0原文

最初我很惊讶 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染墨丶若流云 2024-09-13 12:46:00

看来简单是主要原因。来自本次采访

Gosling:对于我来说,作为一名语言设计师,我现在并不真正认为自己是这样,“简单”真正的最终含义是我是否可以期望 J. Random Developer 将规范牢记在心。例如,这个定义表明,Java 就不是——事实上,许多这些语言最终都会出现很多极端情况,而没有人真正理解这些情况。对任何 C 开发人员进行有关无符号的测验,很快您就会发现几乎没有 C 开发人员真正了解无符号的情况以及无符号算术是什么。诸如此类的事情使 C 变得复杂。我认为 Java 的语言部分非常简单。您必须查找的库。

我最初的假设是,这是因为 Java 根本没有无符号数字类型。为什么byte应该是一个例外? char 是一种特殊情况,因为它必须表示 UTF-16 代码单元(感谢 Jon Skeet 的引用)

It appears that simplicity was the main reason. From this interview:

Gosling: For me as a language designer, which I don't really count myself as these days, what "simple" really ended up meaning was could I expect J. Random Developer to hold the spec in his head. That definition says that, for instance, Java isn't -- and in fact a lot of these languages end up with a lot of corner cases, things that nobody really understands. Quiz any C developer about unsigned, and pretty soon you discover that almost no C developers actually understand what goes on with unsigned, what unsigned arithmetic is. Things like that made C complex. The language part of Java is, I think, pretty simple. The libraries you have to look up.

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)

天生の放荡 2024-09-13 12:46:00

根据“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

问:程序员经常谈论的优点和缺点
用“简单的语言”进行编程。这句话是什么意思
你认为 [C/C++/Java] 是一种简单的语言吗?

里奇:[为了简洁而删除]

Stroustrup:[为了简洁而删除]

Gosling:对于我作为一名语言设计师来说,我并不太重视这一点
如今我自己,“简单”的真正含义是可以
我希望 J. Random Developer 能够牢牢记住该规范。那
例如,定义表明 Java 不是——事实上,很多
这些语言最终会出现很多极端情况,没人知道的事情
真的明白了。对任何 C 开发人员进行有关 unsigned 和 Pretty 的测试
很快你就会发现几乎没有 C 开发人员真正理解什么
继续无符号,什么是无符号算术。诸如此类的事情
使C变得复杂。我认为 Java 的语言部分很漂亮
简单的。您必须查找的库。

另一方面....根据 http://www.artima.com /weblogs/viewpost.jsp?thread=7555

从前有一棵橡树...
作者:海因茨·卡布茨
2003 年 7 月 15 日

...
为了填补我对 Java 历史的空白,我开始深入研究
Sun 的网站,最终偶然发现了 Oak Language
Oak 版本 0.2 的规范。橡树的原名是什么
现在俗称Java,这本手册是最古老的手册
适用于 Oak(即 Java)。
...
无符号整数值(第 3.1 节)

规范中说:“宽度为8、16、32的四种整数类型
和 64 位,并且除非以 unsigned 修饰符作为前缀,否则都是有符号的。

在侧边栏中显示:“unsigned 尚未实现;它可能
永远不会。”你说得多么正确。
“*

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

Q: Programmers often talk about the advantages and disadvantages of
programming in a "simple language." What does that phrase mean to
you, and is [C/C++/Java] a simple language in your view?

Ritchie: [deleted for brevity]

Stroustrup: [deleted for brevity]

Gosling: For me as a language designer, which I don't really count
myself as these days, what "simple" really ended up meaning was could
I expect J. Random Developer to hold the spec in his head. That
definition says that, for instance, Java isn't -- and in fact a lot of
these languages end up with a lot of corner cases, things that nobody
really understands. Quiz any C developer about unsigned, and pretty
soon you discover that almost no C developers actually understand what
goes on with unsigned, what unsigned arithmetic is. Things like that
made C complex. The language part of Java is, I think, pretty
simple. The libraries you have to look up.

On the other hand.... According to http://www.artima.com/weblogs/viewpost.jsp?thread=7555

Once Upon an Oak ...
by Heinz Kabutz
July 15, 2003

...
Trying to fill my gaps of Java's history, I started digging around on
Sun's website, and eventually stumbled across the Oak Language
Specification for Oak version 0.2. Oak was the original name of what
is now commonly known as Java, and this manual is the oldest manual
available for Oak (i.e. Java).
...
Unsigned integer values (Section 3.1)

The specification says: "The four integer types of widths of 8, 16, 32
and 64 bits, and are signed unless prefixed by the unsigned modifier.

In the sidebar it says: "unsigned isn't implemented yet; it might
never be." How right you were.
"*

檐上三寸雪 2024-09-13 12:46:00

我不知道 James Gosling 有任何直接引用,但有一个关于 unsigned byte 的官方 RFE:

错误 ID:4186775:请求无符号整数类型,尤其是。无符号字节

状态:11-已关闭,不会修复,请求增强

请扩展 Java 设计以允许无符号类型,特别是
无符号字节

我一直想知道为什么Java中没有无符号整数类型。它
在我看来,对于 byte 长度的值来说,如果没有
他们[...]

我认识到这是 Java 开发人员做出的设计决策。
我不明白的是为什么。他们是否认为无符号整数类型是邪恶的
或者有害,并选择保护我免受自己的侵害?

I'm not aware of any direct quotes from James Gosling, but there's an official RFE for unsigned byte:

Bug ID: 4186775: request unsigned integer types, esp. unsigned byte

State: 11-Closed, Will Not Fix, request for enhancement

Please extend the Java design to allow unsigned types, particularly
unsigned byte.

I have been wondering why there are no unsigned integer types in Java. It
seems to me that for byte-length values it is extremely awkward not to have
them [...]

I recognize that this was a design decision made by the Java developers.
What I don't understand is why. Did they consider unsigned integer types evil
or harmful, and chose to protect me from myself?

不爱素颜 2024-09-13 12:46:00

字节没有理由被无符号。当您使用 char 类型来表示字符时,byte 通常不会完成 char 的工作。

There's no reason for a byte to be unsigned. when you have char type to represent characters, the byte would normally not do that job of a char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文