Java 的 L 数(长)规范

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

是否有其他数字规格,如短,字节,浮动,双?看起来这些是好的,因为(我假设)如果你可以指定你输入的数字是一个短的,那么 java 就不必强制转换它-这是一个假设,如果我错了请纠正我。我通常会自己搜索这个问题,但我甚至不知道这种数字说明书叫什么。

180753 次浏览

To understand why it is necessary to distinguish between int and long literals, consider:

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 int and long literals, what is the interpretation of -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, short and char because 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. float uses suffix f and double d. Other literals have unambiguous types, with there being a special type for null.

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

There are specific suffixes for long (e.g. 39832L), float (e.g. 2.4f) and double (e.g. -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.

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

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.

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

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 made to add suffixes for byte and shortY and S respectively. 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:

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 };

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

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

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

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

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.

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.

so finally

long l = 2147483648L;// works fine.

Java has two types of data type :

  1. Primitive Data-Type
  2. Non-Primitive Data-Type

Certain data types require specifications like long, float, and double.

While assigning any of the above data types to any variable always remember to....

  • End the value with a "d" in double data type.
  • End the value with a "L" in long data type.
  • End the value with a "f" in float data type.

Example:

long number = 15000000000L;

float mysecondnum = 5.75f;

double mynumber = 19.99d;

More info:

  • The size of the long data type is 8 bytes.
  • The size of the float data type is 4 bytes.
  • The size of the double data type is 8 bytes.

The precision level of the long data type is up to 7-8 decimal points while the float data type is up to 15 decimal points.

Edit:

Along with this typecasting can be used to change the primitive data type from one to another.

  • Widening Casting(automatically): smaller type to a larger type size
  • Narrowing Casting(manually): larger type to a smaller size type