为什么 Java 不支持无符号整数? [英] Why doesn't Java support unsigned ints?

查看:43
本文介绍了为什么 Java 不支持无符号整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Java 不支持无符号整数?

Why doesn't Java include support for unsigned integers?

在我看来,这是一个奇怪的遗漏,因为它们允许编写的代码不太可能在意外大输入时产生溢出.

It seems to me to be an odd omission, given that they allow one to write code that is less likely to produce overflows on unexpectedly large input.

此外,使用无符号整数可以是一种自我证明的形式,因为它们表明无符号整数打算保存的值永远不应该是负数.

Furthermore, using unsigned integers can be a form of self-documentation, since they indicate that the value which the unsigned int was intended to hold is never supposed to be negative.

最后,在某些情况下,无符号整数对于某些运算(例如除法)可能更有效.

Lastly, in some cases, unsigned integers can be more efficient for certain operations, such as division.

包含这些有什么缺点?

推荐答案

这是来自 采访Gosling 和其他人,关于简单性:

This is from an interview with Gosling and others, about simplicity:

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

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.

这篇关于为什么 Java 不支持无符号整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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