📜  java long literal - Java (1)

📅  最后修改于: 2023-12-03 15:15:56.966000             🧑  作者: Mango

Java Long Literal

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.

Syntax

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.

Range

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.

Example Usages
Storing 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
Calculations and conversions
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
Loop iteration
for (long i = 0; i < 10_000L; i++) {
    // Perform some iteration logic
}
Conclusion

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.