Java的L号(长)规范

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/769963/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:16:36  来源:igfitidea点击:

Java's L number (long) specification

javanumberslong-integershortspecifications

提问by jbu

It appears that when you type in a number in Java, the compiler automatically reads it as an integer, which is why when you type in (long) 6000000000(not in integer's range) it will complain that 6000000000is not an integer. To correct this, I had to specify 6000000000L. I just learned about this specification.

看起来,当您在 Java 中输入数字时,编译器会自动将其读取为整数,这就是为什么当您输入 (long) 6000000000(不在整数范围内)时,它会抱怨6000000000不是整数。为了纠正这个问题,我必须指定6000000000L. 我刚刚了解了这个规范。

Are there other number specifications like for short, byte, float, double? It seems like these would be good to have because (I assume) if you could specify the number you're typing in is a short then java wouldn't have to cast it - that is an assumption, correct me if I'm wrong. I would normally search this question myself, but I don't know what this kind of number specification is even called.

是否还有其他数字规范,如 short、byte、float、double?看起来这些会很好,因为(我假设)如果您可以指定您输入的数字是一个简短的那么 java 就不必强制转换它 - 这是一个假设,如果我错了,请纠正我. 我通常会自己搜索这个问题,但我什至不知道这种数字规范叫什么。

采纳答案by Simon Nickerson

There are specific suffixes for long(e.g. 39832L), float(e.g. 2.4f) and double(e.g. -7.832d).

long(eg 39832L)、float(eg 2.4f) 和double(eg -7.832d)有特定的后缀。

If there is no suffix, and it is an integral type (e.g. 5623), it is assumed to be an int. If it is not an integral type (e.g. 3.14159), it is assumed to be a double.

如果没有后缀,并且是整数类型(例如5623),则假定为int。如果它不是整数类型(例如3.14159),则假定它是 a double

In all other cases (byte, short, char), you need the cast as there is no specific suffix.

在所有其他情况下 ( byte, short, char),您需要演员表,因为没有特定的后缀。

The Java spec allows both upper and lower case suffixes, but the upper case version for longs is preferred, as the upper case Lis less easy to confuse with a numeral 1than the lower case l.

Java规范允许上部和下部壳体的后缀,但是上壳体版本为longs的优选的,因为上壳体L不太容易混淆以数字1比下壳体l

See the JLS section 3.10for the gory details (see the definition of IntegerTypeSuffix).

有关详细信息,请参阅JLS 第 3.10 节(请参阅 的定义IntegerTypeSuffix)。

回答by Tom Hawtin - tackline

To understand why it is necessary to distinguish between intand longliterals, consider:

要理解为什么有必要区分intlong文字,请考虑:

long l = -1 >>> 1;

versus

相对

int a = -1;
long l = a >>> 1;

Now as you would rightly expect, both code fragments give the same value to variable l. Without being able to distinguish intand longliterals, what is the interpretation of -1 >>> 1?

现在正如您所期望的那样,两个代码片段都为变量提供了相同的值l。在无法区分intlong字面意思的情况下, 的解释是-1 >>> 1什么?

-1L >>> 1 // ?

or

或者

(int)-1 >>> 1 // ?

So even if the number is in the common range, we need to specify type. If the default changed with magnitude of the literal, then there would be a weird change in the interpretations of expressions just from changing the digits.

所以即使数字在公共范围内,我们也需要指定类型。如果默认值随着文字的大小而改变,那么仅仅改变数字就会对表达式的解释产生奇怪的变化。

This does not occur for byte, shortand charbecause they are always promoted before performing arithmetic and bitwise operations. Arguably their should be integer type suffixes for use in, say, array initialisation expressions, but there isn't. floatuses suffix fand doubled. Other literals have unambiguous types, with there being a special type for null.

这不会发生在byte,short并且char因为它们总是在执行算术和按位运算之前被提升。可以说它们应该是整数类型后缀,用于数组初始化表达式,但没有。float使用后缀fdoubled。其他文字具有明确的类型,有一个特殊类型null.

回答by McDowell

These are literals and are described in section 3.10of the Java language spec.

这些是文字,在 Java 语言规范的第 3.10 节中进行了描述。

回答by erickson

I hope you won't mind a slight tangent, but thought you may be interested to know that besides F(for float), D(for double), and L(for long), a proposal has been madeto add suffixes for byteand shortYand Srespectively. This would eliminate to the need to cast to bytes when using literal syntax for byte (or short) arrays. Quoting the example from the proposal:

我希望你不会介意轻微的切线,但我想你可能有兴趣知道除了F(对于浮动)、D(对于双)和L(对于长)之外,已经提出了为byteshort-YS分别添加后缀的建议. 这将消除在对字节(或短)数组使用文字语法时转换为字节的需要。引用提案中的例子:

MAJOR BENEFIT: Why is the platform better if the proposal is adopted?

cruddy code like

 byte[] stuff = { 0x00, 0x7F, (byte)0x80,  (byte)0xFF};

can be recoded as

 byte[] ufum7 = { 0x00y, 0x7Fy, 0x80y, 0xFFy };

主要好处:如果提案被采纳,为什么平台会更好?

粗糙的代码就像

 byte[] stuff = { 0x00, 0x7F, (byte)0x80,  (byte)0xFF};

可以重新编码为

 byte[] ufum7 = { 0x00y, 0x7Fy, 0x80y, 0xFFy };

Joe Darcy is overseeing Project Coin for Java 7, and his bloghas been an easy way to track these proposals.

Joe Darcy 负责监督 Java 7 的 Project Coin,他的博客是跟踪这些提案的一种简单方法。

回答by Michael Borgwardt

It seems like these would be good to have because (I assume) if you could specify the number you're typing in is a short then java wouldn't have to cast it

看起来这些会很好,因为(我假设)如果你可以指定你输入的数字很短,那么java就不必强制转换它

Since the parsing of literals happens at compile time, this is absolutely irrelevant in regard to performance. The only reason having shortand bytesuffixes would be nice is that it lead to more compact code.

由于文字的解析发生在编译时,这与性能绝对无关。拥有shortbyte后缀会很好的唯一原因是它会导致更紧凑的代码。

回答by Utpal Kumar

By default any integral primitive data type (byte, short, int, long) will be treated as inttype by java compiler. For byteand short, as long as value assigned to them is in their range, there is no problem and no suffix required. If value assigned to byteand shortexceeds their range, explicit type casting is required.

默认情况下,任何完整的原始数据类型(byte、short、int、long)都将被java 编译器视为int类型。对于byteshort,只要分配给它们的值在它们的范围内,就没有问题,不需要后缀。如果分配给byteshort 的值超出了它们的范围,则需要显式类型转换。

Ex:

前任:

byte b = 130; // CE: range is exceeding.

to overcome this perform type casting.

为了克服这个执行类型铸造。

byte b = (byte)130; //valid, but chances of losing data is there.

In case of long data type, it can accept the integer value without any hassle. Suppose we assign like

在长数据类型的情况下,它可以毫无困难地接受整数值。假设我们分配像

Long l = 2147483647; //which is max value of int

in this case no suffix like L/l is required. By default value 2147483647 is considered by java compiler is int type. Internal type casting is done by compiler and int is auto promoted to Long type.

在这种情况下,不需要像 L/l 这样的后缀。默认值 2147483647 被 java 编译器认为是 int 类型。内部类型转换由编译器完成,int 自动提升为 Long 类型。

Long l = 2147483648; //CE: value is treated as int but out of range 

Here we need to put suffix as L to treat the literal 2147483648 as long type by java compiler.

这里我们需要将后缀为L,以便java编译器将文字2147483648视为长型。

so finally

所以最后

Long l = 2147483648L;// works fine.