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 FormModifier 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 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)
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 |
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(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()
Number
public 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
.Comparable
public static int compare(int lhs, int rhs)
int
values.public 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 minus sign "-" 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()
Number
doubleValue
in class Number
public boolean equals(Object o)
o
must be an instance of
Integer
and have the same integer value as this object.equals
in class Object
o
- the object to compare this integer with.true
if the specified object is equal to this
Integer
; false
otherwise.Object.hashCode()
public float floatValue()
Number
floatValue
in class Number
public 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()
Object
Object.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 Object
Object.equals(java.lang.Object)
public int intValue()
public long longValue()
Number
public 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(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 short shortValue()
Number
shortValue
in class Number
public 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()
Object
getClass().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
.