public final class Integer extends Number implements Comparable<Integer>
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.
Long,
Serialized Form| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_VALUE
Constant for the maximum
int value, 231-1. |
static int |
MIN_VALUE
Constant for the minimum
int value, -231. |
static int |
SIZE
Constant for the number of bits needed to represent an
int in
two's complement form. |
static Class<Integer> |
TYPE
The
Class object that represents the primitive type int. |
| Constructor and Description |
|---|
Integer(int value)
Constructs a new
Integer with the specified primitive integer
value. |
Integer(String string)
Constructs a new
Integer from the specified string. |
| Modifier and Type | Method and Description |
|---|---|
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 x,
int y)
Compares two
int values numerically. |
int |
compareTo(Integer object)
Compares this object to the specified integer object to determine their
relative order.
|
static int |
compareUnsigned(int x,
int y)
Compares two
int values numerically treating the values
as unsigned. |
static Integer |
decode(String string)
Parses the specified string and returns a
Integer instance if the
string can be decoded into an integer value. |
static int |
divideUnsigned(int dividend,
int divisor)
Returns the unsigned quotient of dividing the first argument by
the second where each argument and the result is interpreted as
an unsigned 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)
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. |
static Integer |
getInteger(String string,
Integer defaultValue)
Returns the
Integer value of the system property identified by
string. |
int |
hashCode()
Returns an integer hash code for this object.
|
static int |
hashCode(int value)
Returns a hash code for a
int value; compatible with
Integer.hashCode(). |
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 |
max(int a,
int b)
Returns the greater of two
int values
as if by calling Math.max. |
static int |
min(int a,
int b)
Returns the smaller of two
int values
as if by calling Math.min. |
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(CharSequence s,
int beginIndex,
int endIndex,
int radix)
Parses the
CharSequence argument as a signed int in the
specified radix, beginning at the specified beginIndex
and extending to endIndex - 1. |
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 |
parsePositiveInt(String string)
Equivalent to
parsePositiveInt(string, 10). |
static int |
parsePositiveInt(String string,
int radix)
Parses the specified string as a positive integer value using the
specified radix.
|
static int |
remainderUnsigned(int dividend,
int divisor)
Returns the unsigned remainder from dividing the first argument
by the second where each argument and the result is interpreted
as an unsigned value.
|
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 int |
sum(int a,
int b)
Adds two integers together as per the + operator.
|
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 long |
toUnsignedLong(int x)
Converts the argument to a
long by an unsigned
conversion. |
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.
|
static Integer |
valueOf(String string,
int radix)
Parses the specified string as a signed integer value using the specified
radix.
|
public static final int MAX_VALUE
int value, 231-1.public static final int MIN_VALUE
int value, -231.public static final int SIZE
int in
two's complement form.public Integer(int value)
Integer with the specified primitive integer
value.value - the primitive integer value to store in the new instance.public Integer(String string) throws NumberFormatException
Integer from the specified string.string - the string representation of an integer value.NumberFormatException - if string cannot be parsed as an integer value.parseInt(String)public byte byteValue()
Numberpublic int compareTo(Integer object)
compareTo in interface Comparable<Integer>object - the integer object to compare this object to.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.Comparablepublic static Integer decode(String string) throws NumberFormatException
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.string - a string representation of an integer value.Integer containing the value represented by
string.NumberFormatException - if string cannot be parsed as an integer value.public double doubleValue()
NumberdoubleValue in class Numberpublic boolean equals(Object o)
o must be an instance of
Integer and have the same integer value as this object.equals in class Objecto - the object to compare this integer with.true if the specified object is equal to this
Integer; false otherwise.Object.hashCode()public float floatValue()
NumberfloatValue in class Numberpublic static Integer getInteger(String string)
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.string - the name of the requested system property.Integer or
null.public static Integer getInteger(String string, int defaultValue)
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.string - the name of the requested system property.defaultValue - the default value that is returned if there is no integer
system property with the requested name.Integer or the
default value.public static Integer getInteger(String string, Integer defaultValue)
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.string - the name of the requested system property.defaultValue - the default value that is returned if there is no integer
system property with the requested name.Integer or the
default value.public int hashCode()
ObjectObject.equals(java.lang.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.
hashCode in class ObjectObject.equals(java.lang.Object)public int intValue()
public long longValue()
Numberpublic static int parseInt(String string) throws NumberFormatException
string - the string representation of an integer value.string.NumberFormatException - if string cannot be parsed as an integer value.public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException
CharSequence argument as a signed int in the
specified radix, beginning at the specified beginIndex
and extending to endIndex - 1.
The method does not take steps to guard against the
CharSequence being mutated while parsing.
s - the CharSequence containing the int
representation to be parsedbeginIndex - the beginning index, inclusive.endIndex - the ending index, exclusive.radix - the radix to be used while parsing s.int represented by the subsequence in
the specified radix.NullPointerException - if s is null.IndexOutOfBoundsException - if beginIndex is
negative, or if beginIndex is greater than
endIndex or if endIndex is greater than
s.length().NumberFormatException - if the CharSequence does not
contain a parsable int in the specified
radix, or if radix is either smaller than
Character.MIN_RADIX or larger than
Character.MAX_RADIX.public static int parseInt(String string, int radix) throws NumberFormatException
string - the string representation of an integer value.radix - the radix to use when parsing.string using
radix.NumberFormatException - if string cannot be parsed as an integer value,
or radix < Character.MIN_RADIX ||
radix > Character.MAX_RADIX.public static int parsePositiveInt(String string) throws NumberFormatException
parsePositiveInt(string, 10).NumberFormatExceptionparsePositiveInt(String, int)public static int parsePositiveInt(String string, int radix) throws NumberFormatException
This method behaves the same as parseInt(String, int) except
that it disallows leading '+' and '-' characters. See that method for
error conditions.
NumberFormatExceptionparseInt(String, int)public short shortValue()
NumbershortValue in class Numberpublic static String toBinaryString(int i)
i - the integer to convert.i.public static String toHexString(int i)
i - the integer to convert.i.public static String toOctalString(int i)
i - the integer to convert.i.public String toString()
ObjectgetClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString method
if you intend implementing your own toString method.
public static String toString(int i)
i - the integer to convert.i.public static String toString(int i, int radix)
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.
i - the signed integer to convert.radix - the base to use for the conversion.i.public static Integer valueOf(String string) throws NumberFormatException
string - the string representation of an integer value.Integer instance containing the integer value
represented by string.NumberFormatException - if string cannot be parsed as an integer value.parseInt(String)public static Integer valueOf(String string, int radix) throws NumberFormatException
string - the string representation of an integer value.radix - the radix to use when parsing.Integer instance containing the integer value
represented by string using radix.NumberFormatException - if string cannot be parsed as an integer value, or
radix < Character.MIN_RADIX ||
radix > Character.MAX_RADIX.parseInt(String, int)public static int highestOneBit(int i)
i - the integer to examine.i.public static int lowestOneBit(int i)
i - the integer to examine.i.public static int numberOfLeadingZeros(int i)
highest one bit.i - the integer to examine.i.public static int numberOfTrailingZeros(int i)
lowest one bit.i - the integer to examine.i.public static int bitCount(int i)
i - the integer to examine.i.public static int rotateLeft(int i,
int distance)
i - the integer value to rotate left.distance - the number of bits to rotate.public static int rotateRight(int i,
int distance)
i - the integer value to rotate right.distance - the number of bits to rotate.public static int reverseBytes(int i)
i - the integer value for which to reverse the byte order.public static int reverse(int i)
i - the integer value for which to reverse the bit order.public static int signum(int i)
signum function for the specified
integer.i - the integer value to check.i is negative, 1 if i is positive, 0 if
i is zero.public static Integer valueOf(int i)
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.
i - the integer value to store in the instance.Integer instance containing i.public static int hashCode(int value)
int value; compatible with
Integer.hashCode().value - the value to hashint value.public static int compare(int x,
int y)
int values numerically.
The value returned is identical to what would be returned by:
Integer.valueOf(x).compareTo(Integer.valueOf(y))
x - the first int to comparey - the second int to compare0 if x == y;
a value less than 0 if x < y; and
a value greater than 0 if x > ypublic static int compareUnsigned(int x,
int y)
int values numerically treating the values
as unsigned.x - the first int to comparey - the second int to compare0 if x == y; a value less
than 0 if x < y as unsigned values; and
a value greater than 0 if x > y as
unsigned valuespublic static long toUnsignedLong(int x)
long by an unsigned
conversion. In an unsigned conversion to a long, the
high-order 32 bits of the long are zero and the
low-order 32 bits are equal to the bits of the integer
argument.
Consequently, zero and positive int values are mapped
to a numerically equal long value and negative int values are mapped to a long value equal to the
input plus 232.x - the value to convert to an unsigned longlong by an unsigned
conversionpublic static int divideUnsigned(int dividend,
int divisor)
Note that in two's complement arithmetic, the three other
basic arithmetic operations of add, subtract, and multiply are
bit-wise identical if the two operands are regarded as both
being signed or both being unsigned. Therefore separate addUnsigned, etc. methods are not provided.
dividend - the value to be divideddivisor - the value doing the dividingremainderUnsigned(int, int)public static int remainderUnsigned(int dividend,
int divisor)
dividend - the value to be divideddivisor - the value doing the dividingdivideUnsigned(int, int)public static int sum(int a,
int b)
a - the first operandb - the second operanda and bBinaryOperatorpublic static int max(int a,
int b)
int values
as if by calling Math.max.a - the first operandb - the second operanda and bBinaryOperatorpublic static int min(int a,
int b)
int values
as if by calling Math.min.a - the first operandb - the second operanda and bBinaryOperator