Difference Between 1L and (long) 1 – 1L 和(长)1 之间的区别

最后修改: 2024年 1月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll explore the differences between the literal representation of a long type and the conversion of an int value to a long value using the cast operator.

在本教程中,我们将探讨 long 类型的字面表示法与使用 cast 操作符将 int 值转换为 long 值之间的区别。

2. Literal Representation

2.文字表述

By default, Java treats all integral numeric values as of the int type. Similarly, for the floating-point values, the default type is double.

默认情况下,Java 将所有整数数值都视为 int 类型。同样,对于浮点数值,默认类型为 double.

An integral numeric value is considered to be of the long type only if the value contains the letter “L” or “l” as a suffix:

只有在数值后缀包含字母”L“或”l“时,整数数值才被视为类型:

long x = 1L;

However, the lowercase “l” looks like the number 1. To avoid confusion, we should consider using the uppercase version of the letter.

然而,小写字母”l“看起来就像数字1为了避免混淆,我们应该考虑使用大写字母

As we know, each primitive data type has its corresponding wrapper class, which comes with different methods we can use and, additionally, allows us to use primitive values within generics.

我们知道,每个基元数据类型都有其对应的 封装类,该类带有不同的方法供我们使用,此外,它还允许我们在泛型中使用 原始值

Thanks to the Java autoboxing functionality, we can assign the literal value to its wrapper class reference directly:

得益于 Java 的自动排序功能,我们可以直接将字面值赋值给其封装类引用:

Long x = 1L;

Here, Java converts the long value to the Long object automatically.

在这里,Java 会自动将 long 值转换为 Long 对象。

Furthermore, when we define a long value using the literal representation, it always evaluates as a long data type.

此外,当我们使用字面表示法定义 long 值时,它总是作为 long 数据类型进行评估。

3. Explicit Type Casting

3.显式类型转换

Moving forward, let’s examine the cases when we’d need to use explicit type casting. As a quick refresher, the cast operator converts a value of one type into the type defined within parentheses.

接下来,我们来看看需要使用显式类型转换的情况。作为快速复习,投递操作符将一种类型的值转换为括号内定义的类型。

3.1. Primitive Data Types

3.1.原始数据类型

To convert a value from one primitive type to another, we need to perform either widening or narrowing of the primitive data type.

要将一个值从一个基元类型转换到另一个基元类型,我们需要执行基元数据类型的 加宽或缩小

Furthermore, to transform the value of the type that supports a bigger range of numbers, we don’t need to perform any additional action:

此外,要转换支持更大数字范围的类型值,我们不需要执行任何额外的操作:

int x = 1;
long y = x;

However, to fit the value into the type with a smaller numeric range, we need to use explicit type casting:

但是,为了将数值放入数值范围较小的类型中,我们需要使用显式类型转换:

int x = 1;
byte y = (byte) x;

One thing to keep in mind here is the possible data overflow that happens if a number is too big to fit into the new type.

需要注意的一点是,如果一个数字太大,无法放入新类型中,可能会发生数据溢出

3.2. Wrapper Class

3.2.封装类

One way to convert the primitive data type into a wrapper class that doesn’t correlate with the primitive type is by using the cast operator and embracing the Java autoboxing feature:

将基元数据类型转换为与基元类型不相关的封装类的一种方法是使用铸模运算符和 Java 自动装箱功能:

int x = 1;
Long y = (long) x;

Here, we first converted the int value to a long value and then let Java convert the long type to Long using autoboxing.

在这里,我们首先将 int 值转换为 long 值,然后让 Java 使用自动换行将 long 类型转换为 Long 类型。

4. Constant Expression

4.恒定表达

Now that we discussed the basics, let’s dig a little deeper and see how Java treats literal values and casts of primitive data types.

在讨论了基础知识后,让我们深入了解一下 Java 如何处理字面值和原始数据类型的转换。

We typically use the term constant to describe values that don’t change after compilation. On a class level, we define them using the static and final keywords.

我们通常使用常量一词来描述编译后不会改变的值。在类的层面上,我们使用 staticfinal 关键字来定义它们。

However, besides the class constants, Java recognizes expressions that can be computed at compile-time. These expressions are referred to as constant expressions.

然而,除了类常量外,Java 还识别可在 编译时计算的表达式。这些表达式被称为常量表达式。

According to the Java Language Specification (JLS), literal primitive values and casts of primitive data types are both considered constant expressions.

根据 Java 语言规范 (JLS),字面基元值和基元数据类型的转换都被视为常量表达式。

Therefore, Java treats both the literal representation of a long value and the explicit cast of an int data type to a long the same way.

因此,Java 对 long 值的字面表示和 int 数据类型显式转换为 long 的处理方式相同。

5. Comparison Between 1L and (long) 1

5.1L(长)1 的比较

Finally, let’s see how the literal representation of a long type differs from casting an int value to a long.

最后,让我们看看 long 类型的字面表示与将 int 值铸造为 long 有何不同。

Firstly, performance-wise, there’s no difference between those two expressions. They’re both considered constants and are evaluated at the compile-time.

首先,从性能上讲,这两个表达式没有任何区别。它们都被视为常量,并在编译时进行评估。

Moreover, we can compile both statements and compare their bytecode results using, for instance, the javap tool. We’ll notice they’re the same. They both use lconst_1 to push a long constant into the stack.

此外,我们还可以使用 javap 工具编译这两种语句并比较它们的字节码结果。我们会发现它们是一样的。它们都使用 lconst_1 将一个 long 常量推入堆栈。

Secondly, using the literal representation can increase the readability of the code.

其次,使用字面表示法可以提高代码的可读性

Lastly, when we define a constant expression using the literal, the value will always be of a long type. However, when we’re using the cast operator, the number we’re casting is of the int type.

最后,当我们使用字面表达式定义常量表达式时,其值将始终是 long 类型。但是,当我们使用投影操作符时,我们要投影的数字是 int 类型。

Therefore, the following code won’t compile:

因此,以下代码将无法编译:

Long x = (long) 123_456_789_101;

Even though we’re storing the value in the Long data type, the number 123_456_789_101 is of the int type. The code is invalid since the number is outside the integer range.

尽管我们将数值存储在 Long 数据类型中,但数字 123_456_789_101 属于 int 类型。由于数字超出了整数范围,因此代码无效。

On the other hand, the literal representation compiles successfully:

另一方面,字面表示法编译成功:

Long x = 123_456_789_101L;

6. Conclusion

6.结论

In this article, we learned the differences between defining a long value using literal representation and casting an int value to a long.

在本文中,我们了解了使用字面表示法定义 long 值与将 int 值转换为 long 之间的区别。

To sum up, from Java’s point of view, they’re both constant expressions. To put it differently, in both cases, the actual value can be determined at the compile-time. However, using the literal representation of a number comes with some benefits, such as increased readability and preventing possible data overflow.

总之,从 Java 的角度来看,它们都是常量表达式。换句话说,在这两种情况下,实际值都可以在编译时确定。不过,使用数字的字面表示法也有一些好处,例如提高可读性和防止可能的数据溢出