为什么 Java 中没有字节或短字面量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/317816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are there no byte or short literals in Java?
提问by Will Wagner
I can create a literal long by appending an L to the value; why can't I create a literal short or byte in some similar way? Why do I need to use an int literal with a cast?
我可以通过在值上附加一个 L 来创建一个文字 long ;为什么我不能以某种类似的方式创建一个字面的 short 或 byte?为什么我需要使用带有强制转换的 int 文字?
And if the answer is "Because there was no short literal in C", then why are there no short literals in C?
如果答案是“因为 C 中没有短文字”,那么为什么 C 中没有短文字?
This doesn't actually affect my life in any meaningful way; it's easy enough to write (short) 0 instead of 0S or something. But the inconsistency makes me curious; it's one of those things that bother you when you're up late at night. Someone at some point made a design decision to make it possible to enter literals for some of the primitive types, but not for all of them. Why?
这实际上并没有以任何有意义的方式影响我的生活;写(短)0而不是0S或其他东西很容易。但这种不一致让我感到好奇;当你深夜醒来时,这是困扰你的事情之一。有人在某个时候做出了一个设计决定,可以为一些原始类型输入文字,但不是所有类型。为什么?
采纳答案by Julien Oster
In C, intat least was meant to have the "natural" word size of the CPU and longwas probably meant to be the "larger natural" word size (not sure in that last part, but it would also explain why intand longhave the same size on x86).
在 C 中,int至少意味着具有 CPU 的“自然”字大小,并且long可能意味着“更大的自然”字大小(在最后一部分不确定,但它也可以解释为什么int并且long具有相同的大小在 x86 上)。
Now, my guess is: for intand long, there's a natural representation that fits exactly into the machine's registers. On most CPUs however, the smaller types byteand shortwould have to be padded to an intanyway before being used. If that's the case, you can as well have a cast.
现在,我的猜测是:对于intand long,有一个完全适合机器寄存器的自然表示。然而,在大多数 CPU 上,较小的类型byte和short无论如何都必须int在使用之前填充到。如果是这样的话,你也可以有一个演员。
回答by Jon Skeet
I suspect it's a case of "don't add anything to the language unless it really adds value" - and it was seen as adding sufficiently little value to not be worth it. As you've said, it's easy to get round, and frankly it's rarely necessary anyway (only for disambiguation).
我怀疑这是一个“不要向语言添加任何东西,除非它真的增加价值”的情况——而且它被视为增加的价值太小,不值得。正如您所说,这很容易绕过,坦率地说,无论如何都很少需要(仅用于消除歧义)。
The same is true in C#, and I've never particularly missed it in either language. What I do miss in Java is an unsigned byte type :)
在 C# 中也是如此,而且我从未在任何一种语言中特别错过它。我在 Java 中错过的是无符号字节类型 :)
回答by Joachim Sauer
Another reason might be that the JVM doesn't know about short and byte. All calculations and storing is done with ints, longs, floats and doubles inside the JVM.
另一个原因可能是 JVM 不知道 short 和 byte。所有计算和存储都是在 JVM 中使用整数、长整数、浮点数和双精度数完成的。
回答by Barry Feigenbaum
There are several things to consider.
有几件事情需要考虑。
1) As discussed above the JVM has no notion of byte or short types. Generally these types are not used in computation at the JVM level; so one can think there would be less use of these literals.
1) 如上所述,JVM 没有字节或短类型的概念。通常这些类型不用于 JVM 级别的计算;所以人们可以认为这些文字的使用会减少。
2) For initialization of byte and short variables, if the int expression is constant and in the allowed range of the type it is implicitly cast to the target type.
2) 对于 byte 和 short 变量的初始化,如果 int 表达式是常量并且在类型的允许范围内,则将其隐式转换为目标类型。
3) One can always cast the literal, ex (short)10
3) 总是可以转换文字,ex (short)10

