FileDocCategorySizeDatePackage
Long.javaAPI DocAndroid 1.5 API26321Wed May 06 22:41:04 BST 2009java.lang

Long

public final class Long extends Number implements Comparable
The wrapper for the primitive type {@code long}.

As with the specification, this implementation relies on code laid out in Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002) as well as The Aggregate's Magic Algorithms.

see
java.lang.Number
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private final long
value
The value which the receiver represents.
public static final long
MAX_VALUE
Constant for the maximum {@code long} value, 263-1.
public static final long
MIN_VALUE
Constant for the minimum {@code long} value, -263.
public static final Class
TYPE
The {@link Class} object that represents the primitive type {@code long}.
public static final int
SIZE
Constant for the number of bits needed to represent a {@code long} in two's complement form.
Constructors Summary
public Long(long value)
Constructs a new {@code Long} with the specified primitive long value.

param
value the primitive long value to store in the new instance.
since
Android 1.0



                                               
       
        this.value = value;
    
public Long(String string)
Constructs a new {@code Long} from the specified string.

param
string the string representation of a long value.
throws
NumberFormatException if {@code string} can not be decoded into a long value.
see
#parseLong(String)
since
Android 1.0

        this(parseLong(string));
    
Methods Summary
public static intbitCount(long lng)
Counts the number of 1 bits in the specified long value; this is also referred to as population count.

param
lng the long to examine.
return
the number of 1 bits in {@code lng}.
since
Android 1.0

        lng = (lng & 0x5555555555555555L) + ((lng >> 1) & 0x5555555555555555L);
        lng = (lng & 0x3333333333333333L) + ((lng >> 2) & 0x3333333333333333L);
        // adjust for 64-bit integer
        int i = (int) ((lng >>> 32) + lng);
        i = (i & 0x0F0F0F0F) + ((i >> 4) & 0x0F0F0F0F);
        i = (i & 0x00FF00FF) + ((i >> 8) & 0x00FF00FF);
        i = (i & 0x0000FFFF) + ((i >> 16) & 0x0000FFFF);
        return i;
    
public bytebyteValue()

        return (byte) value;
    
public intcompareTo(java.lang.Long object)
Compares this object to the specified long object to determine their relative order.

param
object the long object to compare this object to.
return
a negative value if the value of this long is less than the value of {@code object}; 0 if the value of this long and the value of {@code object} are equal; a positive value if the value of this long is greater than the value of {@code object}.
see
java.lang.Comparable
since
Android 1.0

        return value > object.value ? 1 : (value < object.value ? -1 : 0);
    
public static java.lang.Longdecode(java.lang.String string)
Parses the specified string and returns a {@code Long} instance if the string can be decoded into a long value. The string may be an optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."), octal ("0..."), or decimal ("...") representation of a long.

param
string a string representation of a long value.
return
a {@code Long} containing the value represented by {@code string}.
throws
NumberFormatException if {@code string} can not be parsed as a long value.
since
Android 1.0

        int length = string.length(), i = 0;
        if (length == 0) {
            throw new NumberFormatException();
        }
        char firstDigit = string.charAt(i);
        boolean negative = firstDigit == '-";
        if (negative) {
            if (length == 1) {
                throw new NumberFormatException(string);
            }
            firstDigit = string.charAt(++i);
        }

        int base = 10;
        if (firstDigit == '0") {
            if (++i == length) {
                return valueOf(0L);
            }
            if ((firstDigit = string.charAt(i)) == 'x" || firstDigit == 'X") {
                if (i == length) {
                    throw new NumberFormatException(string);
                }
                i++;
                base = 16;
            } else {
                base = 8;
            }
        } else if (firstDigit == '#") {
            if (i == length) {
                throw new NumberFormatException(string);
            }
            i++;
            base = 16;
        }

        long result = parse(string, i, base, negative);
        return valueOf(result);
    
public doubledoubleValue()

        return value;
    
public booleanequals(java.lang.Object o)
Compares this instance with the specified object and indicates if they are equal. In order to be equal, {@code o} must be an instance of {@code Long} and have the same long value as this object.

param
o the object to compare this long with.
return
{@code true} if the specified object is equal to this {@code Long}; {@code false} otherwise.
since
Android 1.0

        return (o instanceof Long)
                && (value == ((Long) o).value);
    
public floatfloatValue()

        return value;
    
public static java.lang.LonggetLong(java.lang.String string, long defaultValue)
Returns the {@code Long} value of the system property identified by {@code string}. Returns the specified default value if {@code string} is {@code null} or empty, if the property can not be found or if its value can not be parsed as a long.

param
string the name of the requested system property.
param
defaultValue the default value that is returned if there is no long system property with the requested name.
return
the requested property's value as a {@code Long} or the default value.
since
Android 1.0

        if (string == null || string.length() == 0) {
            return valueOf(defaultValue);
        }
        String prop = System.getProperty(string);
        if (prop == null) {
            return valueOf(defaultValue);
        }
        try {
            return decode(prop);
        } catch (NumberFormatException ex) {
            return valueOf(defaultValue);
        }
    
public static java.lang.LonggetLong(java.lang.String string, java.lang.Long defaultValue)
Returns the {@code Long} value of the system property identified by {@code string}. Returns the specified default value if {@code string} is {@code null} or empty, if the property can not be found or if its value can not be parsed as a long.

param
string the name of the requested system property.
param
defaultValue the default value that is returned if there is no long system property with the requested name.
return
the requested property's value as a {@code Long} or the default value.
since
Android 1.0

        if (string == null || string.length() == 0) {
            return defaultValue;
        }
        String prop = System.getProperty(string);
        if (prop == null) {
            return defaultValue;
        }
        try {
            return decode(prop);
        } catch (NumberFormatException ex) {
            return defaultValue;
        }
    
public static java.lang.LonggetLong(java.lang.String string)
Returns the {@code Long} value of the system property identified by {@code string}. Returns {@code null} if {@code string} is {@code null} or empty, if the property can not be found or if its value can not be parsed as a long.

param
string the name of the requested system property.
return
the requested property's value as a {@code Long} or {@code null}.
since
Android 1.0

        if (string == null || string.length() == 0) {
            return null;
        }
        String prop = System.getProperty(string);
        if (prop == null) {
            return null;
        }
        try {
            return decode(prop);
        } catch (NumberFormatException ex) {
            return null;
        }
    
public inthashCode()

        return (int) (value ^ (value >>> 32));
    
public static longhighestOneBit(long lng)
Determines the highest (leftmost) bit of the specified long value that is 1 and returns the bit mask value for that bit. This is also referred to as the Most Significant 1 Bit. Returns zero if the specified long is zero.

param
lng the long to examine.
return
the bit mask indicating the highest 1 bit in {@code lng}.
since
Android 1.0

        lng |= (lng >> 1);
        lng |= (lng >> 2);
        lng |= (lng >> 4);
        lng |= (lng >> 8);
        lng |= (lng >> 16);
        lng |= (lng >> 32);
        return (lng & ~(lng >>> 1));
    
public intintValue()

        return (int) value;
    
public longlongValue()
Gets the primitive value of this long.

return
this object's primitive value.
since
Android 1.0

        return value;
    
public static longlowestOneBit(long lng)
Determines the lowest (rightmost) bit of the specified long value that is 1 and returns the bit mask value for that bit. This is also referred to as the Least Significant 1 Bit. Returns zero if the specified long is zero.

param
lng the long to examine.
return
the bit mask indicating the lowest 1 bit in {@code lng}.
since
Android 1.0

        return (lng & (-lng));
    
public static intnumberOfLeadingZeros(long lng)
Determines the number of leading zeros in the specified long value prior to the {@link #highestOneBit(long) highest one bit}.

param
lng the long to examine.
return
the number of leading zeros in {@code lng}.
since
Android 1.0

        lng |= lng >> 1;
        lng |= lng >> 2;
        lng |= lng >> 4;
        lng |= lng >> 8;
        lng |= lng >> 16;
        lng |= lng >> 32;
        return bitCount(~lng);
    
public static intnumberOfTrailingZeros(long lng)
Determines the number of trailing zeros in the specified long value after the {@link #lowestOneBit(long) lowest one bit}.

param
lng the long to examine.
return
the number of trailing zeros in {@code lng}.
since
Android 1.0

        return bitCount((lng & -lng) - 1);
    
private static longparse(java.lang.String string, int offset, int radix, boolean negative)

        long max = Long.MIN_VALUE / radix;
        long result = 0, length = string.length();
        while (offset < length) {
            int digit = Character.digit(string.charAt(offset++), radix);
            if (digit == -1) {
                throw new NumberFormatException(string);
            }
            if (max > result) {
                throw new NumberFormatException(string);
            }
            long next = result * radix - digit;
            if (next > result) {
                throw new NumberFormatException(string);
            }
            result = next;
        }
        if (!negative) {
            result = -result;
            if (result < 0) {
                throw new NumberFormatException(string);
            }
        }
        return result;
    
public static longparseLong(java.lang.String string)
Parses the specified string as a signed decimal long value. The ASCII character \u002d ('-') is recognized as the minus sign.

param
string the string representation of a long value.
return
the primitive long value represented by {@code string}.
throws
NumberFormatException if {@code string} is {@code null}, has a length of zero or can not be parsed as a long value.
since
Android 1.0

        return parseLong(string, 10);
    
public static longparseLong(java.lang.String string, int radix)
Parses the specified string as a signed long value using the specified radix. The ASCII character \u002d ('-') is recognized as the minus sign.

param
string the string representation of a long value.
param
radix the radix to use when parsing.
return
the primitive long value represented by {@code string} using {@code radix}.
throws
NumberFormatException if {@code string} is {@code null} or has a length of zero, {@code radix < Character.MIN_RADIX}, {@code radix > Character.MAX_RADIX}, or if {@code string} can not be parsed as a long value.
since
Android 1.0

        if (string == null || radix < Character.MIN_RADIX
                || radix > Character.MAX_RADIX) {
            throw new NumberFormatException();
        }
        int length = string.length(), i = 0;
        if (length == 0) {
            throw new NumberFormatException(string);
        }
        boolean negative = string.charAt(i) == '-";
        if (negative && ++i == length) {
            throw new NumberFormatException(string);
        }

        return parse(string, i, radix, negative);
    
public static longreverse(long lng)
Reverses the order of the bits of the specified long value.

param
lng the long value for which to reverse the bit order.
return
the reversed value.
since
Android 1.0

        // From Hacker's Delight, 7-1, Figure 7-1
        lng = (lng & 0x5555555555555555L) << 1 | (lng >> 1)
                & 0x5555555555555555L;
        lng = (lng & 0x3333333333333333L) << 2 | (lng >> 2)
                & 0x3333333333333333L;
        lng = (lng & 0x0F0F0F0F0F0F0F0FL) << 4 | (lng >> 4)
                & 0x0F0F0F0F0F0F0F0FL;
        return reverseBytes(lng);
    
public static longreverseBytes(long lng)
Reverses the order of the bytes of the specified long value.

param
lng the long value for which to reverse the byte order.
return
the reversed value.
since
Android 1.0

        long b7 = lng >>> 56;
        long b6 = (lng >>> 40) & 0xFF00L;
        long b5 = (lng >>> 24) & 0xFF0000L;
        long b4 = (lng >>> 8) & 0xFF000000L;
        long b3 = (lng & 0xFF000000L) << 8;
        long b2 = (lng & 0xFF0000L) << 24;
        long b1 = (lng & 0xFF00L) << 40;
        long b0 = lng << 56;
        return (b0 | b1 | b2 | b3 | b4 | b5 | b6 | b7);
    
public static longrotateLeft(long lng, int distance)
Rotates the bits of the specified long value to the left by the specified number of bits.

param
lng the long value to rotate left.
param
distance the number of bits to rotate.
return
the rotated value.
since
Android 1.0

        if (distance == 0) {
            return lng;
        }
        /*
         * According to JLS3, 15.19, the right operand of a shift is always
         * implicitly masked with 0x3F, which the negation of 'distance' is
         * taking advantage of.
         */
        return ((lng << distance) | (lng >>> (-distance)));
    
public static longrotateRight(long lng, int distance)
Rotates the bits of the specified long value to the right by the specified number of bits.

param
lng the long value to rotate right.
param
distance the number of bits to rotate.
return
the rotated value.
since
Android 1.0

        if (distance == 0) {
            return lng;
        }
        /*
         * According to JLS3, 15.19, the right operand of a shift is always
         * implicitly masked with 0x3F, which the negation of 'distance' is
         * taking advantage of.
         */
        return ((lng >>> distance) | (lng << (-distance)));
    
public shortshortValue()

        return (short) value;
    
public static intsignum(long lng)
Returns the value of the {@code signum} function for the specified long value.

param
lng the long value to check.
return
-1 if {@code lng} is negative, 1 if {@code lng} is positive, 0 if {@code lng} is zero.
since
Android 1.0

        return (lng == 0 ? 0 : (lng < 0 ? -1 : 1));
    
public static java.lang.StringtoBinaryString(long l)
Converts the specified long value into its binary string representation. The returned string is a concatenation of '0' and '1' characters.

param
l the long value to convert.
return
the binary string representation of {@code l}.
since
Android 1.0

        int count = 1;
        long j = l;

        if (l < 0) {
            count = 64;
        } else {
            while ((j >>= 1) != 0) {
                count++;
            }
        }

        char[] buffer = new char[count];
        do {
            buffer[--count] = (char) ((l & 1) + '0");
            l >>= 1;
        } while (count > 0);
        return new String(0, buffer.length, buffer);
    
public static java.lang.StringtoHexString(long l)
Converts the specified long value into its hexadecimal string representation. The returned string is a concatenation of characters from '0' to '9' and 'a' to 'f'.

param
l the long value to convert.
return
the hexadecimal string representation of {@code l}.
since
Android 1.0

        int count = 1;
        long j = l;

        if (l < 0) {
            count = 16;
        } else {
            while ((j >>= 4) != 0) {
                count++;
            }
        }

        char[] buffer = new char[count];
        do {
            int t = (int) (l & 15);
            if (t > 9) {
                t = t - 10 + 'a";
            } else {
                t += '0";
            }
            buffer[--count] = (char) t;
            l >>= 4;
        } while (count > 0);
        return new String(0, buffer.length, buffer);
    
public static java.lang.StringtoOctalString(long l)
Converts the specified long value into its octal string representation. The returned string is a concatenation of characters from '0' to '7'.

param
l the long value to convert.
return
the octal string representation of {@code l}.
since
Android 1.0

        int count = 1;
        long j = l;

        if (l < 0) {
            count = 22;
        } else {
            while ((j >>>= 3) != 0) {
                count++;
            }
        }

        char[] buffer = new char[count];
        do {
            buffer[--count] = (char) ((l & 7) + '0");
            l >>>= 3;
        } while (count > 0);
        return new String(0, buffer.length, buffer);
    
public java.lang.StringtoString()

        return Long.toString(value);
    
public static java.lang.StringtoString(long l)
Converts the specified long value into its decimal string representation. The returned string is a concatenation of a minus sign if the number is negative and characters from '0' to '9'.

param
l the long to convert.
return
the decimal string representation of {@code l}.
since
Android 1.0

        return toString(l, 10);
    
public static java.lang.StringtoString(long l, int radix)
Converts the specified long value into a string representation based on the specified radix. The returned string is a concatenation of a minus sign if the number is negative and characters from '0' to '9' and 'a' to 'z', depending on the radix. If {@code radix} is not in the interval defined by {@code Character.MIN_RADIX} and {@code Character.MAX_RADIX} then 10 is used as the base for the conversion.

param
l the long to convert.
param
radix the base to use for the conversion.
return
the string representation of {@code l}.
since
Android 1.0

        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
            radix = 10;
        }
        if (l == 0) {
            return "0"; //$NON-NLS-1$
        }

        int count = 2;
        long j = l;
        boolean negative = l < 0;
        if (!negative) {
            count = 1;
            j = -l;
        }
        while ((l /= radix) != 0) {
            count++;
        }

        char[] buffer = new char[count];
        do {
            int ch = 0 - (int) (j % radix);
            if (ch > 9) {
                ch = ch - 10 + 'a";
            } else {
                ch += '0";
            }
            buffer[--count] = (char) ch;
        } while ((j /= radix) != 0);
        if (negative) {
            buffer[0] = '-";
        }
        return new String(0, buffer.length, buffer);
    
public static java.lang.LongvalueOf(java.lang.String string)
Parses the specified string as a signed decimal long value.

param
string the string representation of a long value.
return
a {@code Long} instance containing the long value represented by {@code string}.
throws
NumberFormatException if {@code string} is {@code null}, has a length of zero or can not be parsed as a long value.
see
#parseLong(String)
since
Android 1.0

        return valueOf(parseLong(string));
    
public static java.lang.LongvalueOf(java.lang.String string, int radix)
Parses the specified string as a signed long value using the specified radix.

param
string the string representation of a long value.
param
radix the radix to use when parsing.
return
a {@code Long} instance containing the long value represented by {@code string} using {@code radix}.
throws
NumberFormatException if {@code string} is {@code null} or has a length of zero, {@code radix < Character.MIN_RADIX}, {@code radix > Character.MAX_RADIX}, or if {@code string} can not be parsed as a long value.
see
#parseLong(String, int)
since
Android 1.0

        return valueOf(parseLong(string, radix));
    
public static java.lang.LongvalueOf(long lng)
Returns a {@code Long} instance for the specified long value.

If it is not necessary to get a new {@code Long} instance, it is recommended to use this method instead of the constructor, since it maintains a cache of instances which may result in better performance.

param
lng the long value to store in the instance.
return
a {@code Long} instance containing {@code lng}.
since
Android 1.0

        if (lng < -128 || lng > 127) {
            return new Long(lng);
        }
        return valueOfCache.CACHE[128+(int)lng];