Java 的 L 号(长)规范

Java#39;s L number (long) specification(Java 的 L 号(长)规范)

本文介绍了Java 的 L 号(长)规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 6000000000 is not an integer. To correct this, I had to specify 6000000000L. I just learned about this specification.

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

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.

推荐答案

long有特定的后缀(如39832L),float(例如2.4f)和double(例如-7.832d).

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

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

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.

在所有其他情况下(byteshortchar),您需要强制转换,因为没有特定的后缀.

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

Java 规范允许大写和小写后缀,但 long 的大写版本是首选,因为大写 L 不太容易混淆一个数字 1 比小写 l.

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

请参阅 JLS 部分 3.10 血淋淋的细节(参见 IntegerTypeSuffix 的定义).

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

这篇关于Java 的 L 号(长)规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java 的 L 号(长)规范

基础教程推荐