# Long class

The Long class wraps a value of long in an object. Long class provides several methods for converting a long to a String and a String to a long.

Constants value from Long class

`static long MAX_VALUE`

- Maximum value a long can have, 2
^{63}-1. `static long MIN_VALUE`

- Minimum value a long can have, -2
^{63}. `static int SIZE`

- The number of bits used to represent a long value in two's complement binary form.

Constructors from Long

`Long(long value)`

- Creates a Long object from long argument.
`Long(String s)`

- Creates a Long object from the long value indicated by the String parameter.

Convert long value to byte, double, float, int, long, short

`byte byteValue()`

- Returns the value of this Long as a byte.
`double doubleValue()`

- Returns the value of this Long as a double.
`float floatValue()`

- Returns the value of this Long as a float.
`int intValue()`

- Returns the value of this Long as an int.
`long longValue()`

- Returns the value of this Long as a long value.
`short shortValue()`

- Returns the value of this Long as a short.

Decode a string to create long value

`static Long decode(String nm)`

- Decodes a String into a Long.

Get the long value from a string

`static Long getLong(String nm)`

- Determines the long value of the system property with the specified name.
`static Long getLong(String nm, long val)`

- Determines the long value of the system property with the specified name.
`static Long getLong(String nm, Long val)`

- Returns the long value of the system property with the specified name.

Compare two long values

`int compareTo(Long anotherLong)`

- Compares two Long objects numerically.
`boolean equals(Object obj)`

- Compares this object to the specified object.

Parse long value from string

`static long parseLong(String s)`

- Parses the string argument as a signed decimal long.
`static long parseLong(String s, int radix)`

- Parses the string as a signed long in the radix.
`static Long valueOf(long l)`

- Returns a Long instance representing the specified long value.
`static Long valueOf(String s)`

- Returns a Long object holding the value of the specified String.
`static Long valueOf(String s, int radix)`

- Returns a Long object holding the value extracted from the String with the radix.

Convert long value to binary, hex and octal format strings

`static String toBinaryString(long i)`

- Returns a string representation of the long argument as an unsigned integer in base 2.
`static String toHexString(long i)`

- Returns a string representation of the long argument as an unsigned integer in base 16.
`static String toOctalString(long i)`

- Returns a string representation of the long argument as an unsigned integer in base 8.

Convert long value to string

`String toString()`

- Returns a String object representing this Long's value.
`static String toString(long i)`

- Returns a String object representing the specified long.
`static String toString(long i, int radix)`

- Returns a string representation of the first argument in the radix.

Get the sign of the long value

`static int signum(long i)`

- Returns the signum function of the specified long value.

Get the number of zero bits preceding and following

`static int numberOfLeadingZeros(long i)`

- Returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation.
`static int numberOfTrailingZeros(long i)`

- Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation.

Reverse and rotate a long value

`static long reverse(long i)`

- Returns the value obtained by reversing the order of the bits in the two's complement binary representation.
`static long reverseBytes(long i)`

- Returns the value obtained by reversing the order of the bytes in the two's complement representation.
`static long rotateLeft(long i, int distance)`

- Returns the value obtained by rotating the two's complement binary representation left by the number of bits.
`static long rotateRight(long i, int distance)`

- Returns the value obtained by rotating the two's complement binary representation right by the number of bits.

Bit oriented operation on long value

`static int bitCount(long i)`

- Returns the number of one-bits in the two's complement binary representation of the specified long value.
`static long highestOneBit(long i)`

- Returns a long value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit.
`static long lowestOneBit(long i)`

- Returns a long value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit.

Home

Java Book

Essential Classes

Java Book

Essential Classes

Long:

- Long class
- Constants value from Long class
- Constructors from Long
- Convert long value to byte, double, float, int, long, short
- Decode a string to create long value
- Get the long value from a system property
- Compare two long values
- Parse long value from string
- Convert long value to binary, hex and octal format strings
- Convert long value to string
- Get the sign of the long value
- Get the number of zero bits preceding and following
- Reverse and rotate a long value