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

查看:29
本文介绍了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号(长)规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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