Java Reference ยท java.lang
The Long class
Reference for java.lang.Long: the long wrapper, constants, parsing, L suffix, hashing, unsigned helpers, boxing and caching, and overflow behavior.
Long wraps the 64-bit primitive long in an object and bundles static helpers for parsing, formatting, comparing and bit work. A long holds values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, which is plenty for timestamps in milliseconds, database ids, file sizes and counters that would overflow an int.
Everything that applies to Integer applies here too: the class is immutable, extends Number, implements Comparable<Long>, and is boxed and unboxed automatically. The two most common mistakes are forgetting the L suffix on literals and comparing boxed values with ==.
long big = 3_000_000_000L; // L is required: 3_000_000_000 is not an int Long boxed = big;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class Long extends Number implements Comparable<Long>
| Member | Description |
|---|---|
MAX_VALUE, MIN_VALUE |
The extreme values of long. |
SIZE, BYTES |
64 and 8. |
valueOf(long), valueOf(String) |
Returns a Long; -128 to 127 are cached. |
parseLong(String), parseLong(String, int radix) |
Text to long; throws NumberFormatException. |
toString(long), toBinaryString, toHexString, toOctalString |
Conversion to text, unsigned for binary/hex/octal. |
compare(long, long), compareTo(Long) |
Ordering. |
sum, max, min |
Method references for reductions. |
bitCount, numberOfLeadingZeros, numberOfTrailingZeros, rotateLeft, reverse |
Bit operations. |
hashCode(long) |
(int) (value ^ (value >>> 32)). |
parseUnsignedLong, toUnsignedString, divideUnsigned, remainderUnsigned, compareUnsigned |
Unsigned interpretation. |
Throws: NumberFormatException for bad text; NullPointerException when unboxing null.
Usage notes
- Literals need
L.long x = 3000000000;does not compile ("integer number too large"). Prefer the uppercaseL; lowercasellooks like1. - Mixed arithmetic.
int * intis computed inintand may overflow before being widened:long ms = 60 * 60 * 24 * 365 * 1000is wrong. Make one operandlong. ==onLongobjects compares identity; only -128..127 are cached. Useequals()or unbox.equalsis type-strict.Long.valueOf(7).equals(7)isfalse, because the argument is boxed toInteger. Write7L.- Atomicity. Reads and writes of a plain
longare not guaranteed atomic on 32-bit JVMs unless the field isvolatile. UseAtomicLongorLongAdderfor shared counters. - Timestamps.
System.currentTimeMillis()returns along. For dates preferjava.timetypes. - Overflow wraps silently, like
int. Use theMathexact methods orBigIntegerwhere it matters. - Precision loss. Converting a
longabove 2^53 todoublerounds it, so do money and ids inlongorBigDecimal, neverdouble.