Android APIs
public final class

Integer

extends Number
implements Comparable<Integer>
java.lang.Object
   ↳ java.lang.Number
     ↳ java.lang.Integer

Class Overview

The wrapper for the primitive type int.

Implementation note: The "bit twiddling" methods in this class use techniques described in Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002) and Sean Anderson's Bit Twiddling Hacks.

See also:

Summary

Constants
int MAX_VALUE Constant for the maximum int value, 231-1.
int MIN_VALUE Constant for the minimum int value, -231.
int SIZE Constant for the number of bits needed to represent an int in two's complement form.
Fields
public static final Class<Integer> TYPE The Class object that represents the primitive type int.
Public Constructors
Integer(int value)
Constructs a new Integer with the specified primitive integer value.
Integer(String string)
Constructs a new Integer from the specified string.
Public Methods
static int bitCount(int i)
Counts the number of 1 bits in the specified integer; this is also referred to as population count.
byte byteValue()
Returns this object's value as a byte.
static int compare(int lhs, int rhs)
Compares two int values.
int compareTo(Integer object)
Compares this object to the specified integer object to determine their relative order.
static Integer decode(String string)
Parses the specified string and returns a Integer instance if the string can be decoded into an integer value.
double doubleValue()
Returns this object's value as a double.
boolean equals(Object o)
Compares this instance with the specified object and indicates if they are equal.
float floatValue()
Returns this object's value as a float.
static Integer getInteger(String string, Integer defaultValue)
Returns the Integer value of the system property identified by string.
static Integer getInteger(String string)
Returns the Integer value of the system property identified by string.
static Integer getInteger(String string, int defaultValue)
Returns the Integer value of the system property identified by string.
int hashCode()
Returns an integer hash code for this object.
static int highestOneBit(int i)
Determines the highest (leftmost) bit of the specified integer that is 1 and returns the bit mask value for that bit.
int intValue()
Gets the primitive value of this int.
long longValue()
Returns this object's value as a long.
static int lowestOneBit(int i)
Determines the lowest (rightmost) bit of the specified integer that is 1 and returns the bit mask value for that bit.
static int numberOfLeadingZeros(int i)
Determines the number of leading zeros in the specified integer prior to the highest one bit.
static int numberOfTrailingZeros(int i)
Determines the number of trailing zeros in the specified integer after the lowest one bit.
static int parseInt(String string)
Parses the specified string as a signed decimal integer value.
static int parseInt(String string, int radix)
Parses the specified string as a signed integer value using the specified radix.
static int reverse(int i)
Reverses the order of the bits of the specified integer.
static int reverseBytes(int i)
Reverses the order of the bytes of the specified integer.
static int rotateLeft(int i, int distance)
Rotates the bits of the specified integer to the left by the specified number of bits.
static int rotateRight(int i, int distance)
Rotates the bits of the specified integer to the right by the specified number of bits.
short shortValue()
Returns this object's value as a short.
static int signum(int i)
Returns the value of the signum function for the specified integer.
static String toBinaryString(int i)
Converts the specified integer into its binary string representation.
static String toHexString(int i)
Converts the specified integer into its hexadecimal string representation.
static String toOctalString(int i)
Converts the specified integer into its octal string representation.
String toString()
Returns a string containing a concise, human-readable description of this object.
static String toString(int i)
Converts the specified integer into its decimal string representation.
static String toString(int i, int radix)
Converts the specified signed integer into a string representation based on the specified radix.
static Integer valueOf(String string, int radix)
Parses the specified string as a signed integer value using the specified radix.
static Integer valueOf(int i)
Returns a Integer instance for the specified integer value.
static Integer valueOf(String string)
Parses the specified string as a signed decimal integer value.
[Expand]
Inherited Methods
From class java.lang.Number
From class java.lang.Object
From interface java.lang.Comparable

Constants

public static final int MAX_VALUE

Added in API level 1

Constant for the maximum int value, 231-1.

Constant Value: 2147483647 (0x7fffffff)

public static final int MIN_VALUE

Added in API level 1

Constant for the minimum int value, -231.

Constant Value: -2147483648 (0x80000000)

public static final int SIZE

Added in API level 1

Constant for the number of bits needed to represent an int in two's complement form.

Constant Value: 32 (0x00000020)

Fields

public static final Class<Integer> TYPE

Added in API level 1

The Class object that represents the primitive type int.

Public Constructors

public Integer (int value)

Added in API level 1

Constructs a new Integer with the specified primitive integer value.

Parameters
value int: the primitive integer value to store in the new instance.

public Integer (String string)

Added in API level 1

Constructs a new Integer from the specified string.

Parameters
string String: the string representation of an integer value.
Throws
NumberFormatException if string cannot be parsed as an integer value.

See also:

Public Methods

public static int bitCount (int i)

Added in API level 1

Counts the number of 1 bits in the specified integer; this is also referred to as population count.

Parameters
i int: the integer to examine.
Returns
int the number of 1 bits in i.

public byte byteValue ()

Added in API level 1

Returns this object's value as a byte. Might involve rounding and/or truncating the value, so it fits into a byte.

Returns
byte the primitive byte value of this object.

public static int compare (int lhs, int rhs)

Added in API level 19

Compares two int values.

Parameters
lhs int
rhs int
Returns
int 0 if lhs = rhs, less than 0 if lhs < rhs, and greater than 0 if lhs > rhs.

public int compareTo (Integer object)

Added in API level 1

Compares this object to the specified integer object to determine their relative order.

Parameters
object Integer: the integer object to compare this object to.
Returns
int a negative value if the value of this integer is less than the value of object; 0 if the value of this integer and the value of object are equal; a positive value if the value of this integer is greater than the value of object.

See also:

public static Integer decode (String string)

Added in API level 1

Parses the specified string and returns a Integer instance if the string can be decoded into an integer value. The string may be an optional sign character ("-" or "+") followed by a hexadecimal ("0x..." or "#..."), octal ("0..."), or decimal ("...") representation of an integer.

Parameters
string String: a string representation of an integer value.
Returns
Integer an Integer containing the value represented by string.
Throws
NumberFormatException if string cannot be parsed as an integer value.

public double doubleValue ()

Added in API level 1

Returns this object's value as a double. Might involve rounding.

Returns
double the primitive double value of this object.

public boolean equals (Object o)

Added in API level 1

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must be an instance of Integer and have the same integer value as this object.

Parameters
o Object: the object to compare this integer with.
Returns
boolean true if the specified object is equal to this Integer; false otherwise.

public float floatValue ()

Added in API level 1

Returns this object's value as a float. Might involve rounding.

Returns
float the primitive float value of this object.

public static Integer getInteger (String string, Integer defaultValue)

Added in API level 1

Returns the Integer value of the system property identified by string. Returns the specified default value if string is null or empty, if the property can not be found or if its value can not be parsed as an integer.

Parameters
string String: the name of the requested system property.
defaultValue Integer: the default value that is returned if there is no integer system property with the requested name.
Returns
Integer the requested property's value as an Integer or the default value.

public static Integer getInteger (String string)

Added in API level 1

Returns the Integer value of the system property identified by string. Returns null if string is null or empty, if the property can not be found or if its value can not be parsed as an integer.

Parameters
string String: the name of the requested system property.
Returns
Integer the requested property's value as an Integer or null.

public static Integer getInteger (String string, int defaultValue)

Added in API level 1

Returns the Integer value of the system property identified by string. Returns the specified default value if string is null or empty, if the property can not be found or if its value can not be parsed as an integer.

Parameters
string String: the name of the requested system property.
defaultValue int: the default value that is returned if there is no integer system property with the requested name.
Returns
Integer the requested property's value as an Integer or the default value.

public int hashCode ()

Added in API level 1

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
int this object's hash code.

public static int highestOneBit (int i)

Added in API level 1

Determines the highest (leftmost) bit of the specified integer 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 integer is zero.

Parameters
i int: the integer to examine.
Returns
int the bit mask indicating the highest 1 bit in i.

public int intValue ()

Added in API level 1

Gets the primitive value of this int.

Returns
int this object's primitive value.

public long longValue ()

Added in API level 1

Returns this object's value as a long. Might involve rounding and/or truncating the value, so it fits into a long.

Returns
long the primitive long value of this object.

public static int lowestOneBit (int i)

Added in API level 1

Determines the lowest (rightmost) bit of the specified integer 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 integer is zero.

Parameters
i int: the integer to examine.
Returns
int the bit mask indicating the lowest 1 bit in i.

public static int numberOfLeadingZeros (int i)

Added in API level 1

Determines the number of leading zeros in the specified integer prior to the highest one bit.

Parameters
i int: the integer to examine.
Returns
int the number of leading zeros in i.

public static int numberOfTrailingZeros (int i)

Added in API level 1

Determines the number of trailing zeros in the specified integer after the lowest one bit.

Parameters
i int: the integer to examine.
Returns
int the number of trailing zeros in i.

public static int parseInt (String string)

Added in API level 1

Parses the specified string as a signed decimal integer value. The ASCII characters - ('-') and + ('+') are recognized as the minus and plus signs.

Parameters
string String: the string representation of an integer value.
Returns
int the primitive integer value represented by string.
Throws
NumberFormatException if string cannot be parsed as an integer value.

public static int parseInt (String string, int radix)

Added in API level 1

Parses the specified string as a signed integer value using the specified radix. The ASCII characters - ('-') and + ('+') are recognized as the minus and plus signs.

Parameters
string String: the string representation of an integer value.
radix int: the radix to use when parsing.
Returns
int the primitive integer value represented by string using radix.
Throws
NumberFormatException if string cannot be parsed as an integer value, or radix < Character.MIN_RADIX || radix > Character.MAX_RADIX.

public static int reverse (int i)

Added in API level 1

Reverses the order of the bits of the specified integer.

Parameters
i int: the integer value for which to reverse the bit order.
Returns
int the reversed value.

public static int reverseBytes (int i)

Added in API level 1

Reverses the order of the bytes of the specified integer.

Parameters
i int: the integer value for which to reverse the byte order.
Returns
int the reversed value.

public static int rotateLeft (int i, int distance)

Added in API level 1

Rotates the bits of the specified integer to the left by the specified number of bits.

Parameters
i int: the integer value to rotate left.
distance int: the number of bits to rotate.
Returns
int the rotated value.

public static int rotateRight (int i, int distance)

Added in API level 1

Rotates the bits of the specified integer to the right by the specified number of bits.

Parameters
i int: the integer value to rotate right.
distance int: the number of bits to rotate.
Returns
int the rotated value.

public short shortValue ()

Added in API level 1

Returns this object's value as a short. Might involve rounding and/or truncating the value, so it fits into a short.

Returns
short the primitive short value of this object.

public static int signum (int i)

Added in API level 1

Returns the value of the signum function for the specified integer.

Parameters
i int: the integer value to check.
Returns
int -1 if i is negative, 1 if i is positive, 0 if i is zero.

public static String toBinaryString (int i)

Added in API level 1

Converts the specified integer into its binary string representation. The returned string is a concatenation of '0' and '1' characters.

Parameters
i int: the integer to convert.
Returns
String the binary string representation of i.

public static String toHexString (int i)

Added in API level 1

Converts the specified integer into its hexadecimal string representation. The returned string is a concatenation of characters from '0' to '9' and 'a' to 'f'.

Parameters
i int: the integer to convert.
Returns
String the hexadecimal string representation of i.

public static String toOctalString (int i)

Added in API level 1

Converts the specified integer into its octal string representation. The returned string is a concatenation of characters from '0' to '7'.

Parameters
i int: the integer to convert.
Returns
String the octal string representation of i.

public String toString ()

Added in API level 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
String a printable representation of this object.

public static String toString (int i)

Added in API level 1

Converts the specified integer 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'.

Parameters
i int: the integer to convert.
Returns
String the decimal string representation of i.

public static String toString (int i, int radix)

Added in API level 1

Converts the specified signed integer 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 radix is not in the interval defined by Character.MIN_RADIX and Character.MAX_RADIX then 10 is used as the base for the conversion.

This method treats its argument as signed. If you want to convert an unsigned value to one of the common non-decimal bases, you may find toBinaryString(int), #toHexString, or toOctalString(int) more convenient.

Parameters
i int: the signed integer to convert.
radix int: the base to use for the conversion.
Returns
String the string representation of i.

public static Integer valueOf (String string, int radix)

Added in API level 1

Parses the specified string as a signed integer value using the specified radix.

Parameters
string String: the string representation of an integer value.
radix int: the radix to use when parsing.
Returns
Integer an Integer instance containing the integer value represented by string using radix.
Throws
NumberFormatException if string cannot be parsed as an integer value, or radix < Character.MIN_RADIX || radix > Character.MAX_RADIX.

public static Integer valueOf (int i)

Added in API level 1

Returns a Integer instance for the specified integer value.

If it is not necessary to get a new Integer 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.

Parameters
i int: the integer value to store in the instance.
Returns
Integer a Integer instance containing i.

public static Integer valueOf (String string)

Added in API level 1

Parses the specified string as a signed decimal integer value.

Parameters
string String: the string representation of an integer value.
Returns
Integer an Integer instance containing the integer value represented by string.
Throws
NumberFormatException if string cannot be parsed as an integer value.

See also: