Java的L号(长)规范 [英] Java's L number (long) specification

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

问题描述

看来,当你在Java中输入一个数字时,编译器会自动将其读作整数,这就是当你输入(长) 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.

在所有其他情况下(字节 char ),你需要强制转换,因为没有特定的后缀。

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

Java规范允许大写和小写后缀,但是首选 long s的大写版本,因为大写 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.

参见< a href =http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10 =noreferrer> JLS第3.10节为血腥详细信息(请参阅 IntegerTypeSuffix 的定义)。

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

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

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