📅  最后修改于: 2023-12-03 15:15:56.966000             🧑  作者: Mango
In Java, a long
literal is used to represent a 64-bit signed integer value. It is used when you need to work with integer numbers that are larger than the range supported by the int
type.
To denote a long
literal value in Java, you can append either an L
or l
at the end of the number.
long myLong = 123456789L;
long anotherLong = 987654321l;
Note that using l
is not recommended since it may be mistaken for the numeric value 1
. It is more common and recommended to use L
to represent long
literals.
The long
type in Java has a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). This gives you the ability to work with extremely large numbers.
long population = 7827000000L; // World population as of 2021
long distanceToSun = 149600000L; // Distance to the Sun in kilometers
long totalRevenue = 234567890123456789L; // An example large revenue value
long a = 123456789L;
long b = 987654321L;
long sum = a + b;
long difference = b - a;
int aToInt = (int) a; // Casting a long to int
double aToDouble = (double) a; // Casting a long to double
for (long i = 0; i < 10_000L; i++) {
// Perform some iteration logic
}
Using the long
data type and long
literals in Java allows programmers to work with large integer values and perform calculations on them. It is important to note the range of long
and use the appropriate literal suffix (L
or l
) when declaring long
literals.