Long.parseLong()
Converts a string with an integer to a long — for numbers that do not fit in an int.
Available since: Java 1.0
Syntax
static long parseLong(String s)Parameters
| Parameter | Type | Description |
|---|---|---|
| s | String | A string with an integer. |
Return value
longThe number as a long.
Examples
Large numbers and long literals with the letter L:
void main() {
long population = Long.parseLong("8100000000");
long distance = 9_460_730_472_580L;
IO.println(population);
IO.println(distance / 1000);
}Output:
8100000000
9460730472int overflow in a multiplication, and the correct version:
void main() {
int ms = 30 * 24 * 60 * 60 * 1000;
long msLong = 30L * 24 * 60 * 60 * 1000;
IO.println(ms);
IO.println(msLong);
}Output:
-1702967296
2592000000Notes
A literal larger than Integer.MAX_VALUE without the L suffix does not compile: "integer number too large". In an expression at least one number must be a long from the start, otherwise the overflow happens before the result becomes long. Underscores in numbers (1_000_000) are just for readability.
See also
- Integer.parseInt() — Converts a string containing an integer to an int.
- Integer.MAX_VALUE — The largest int value; its partner is Integer.MIN_VALUE.
- BigInteger — An integer of any size — for when even long is not enough.
In the tutorial
- Primitive types — Java's eight primitive types: int, long, double, float, char, boolean, byte and short, their ranges and overflow.