Class Integer
- All Implemented Interfaces:
Serializable,Comparable<Integer>
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.
- Since:
- 1.0
- See Also:
Long, Serialized Form
-
Field Summary
Fields Modifier and Type Field Description static intMAX_VALUEConstant for the maximumintvalue, 231-1.static intMIN_VALUEConstant for the minimumintvalue, -231.static intSIZEConstant for the number of bits needed to represent anintin two's complement form.static Class<Integer>TYPETheClassobject that represents the primitive typeint. -
Constructor Summary
-
Method Summary
Modifier and Type Method Description static intbitCount(int i)Counts the number of 1 bits in the specified integer; this is also referred to as population count.bytebyteValue()Returns this object's value as a byte.static intcompare(int lhs, int rhs)Compares twointvalues.intcompareTo(Integer object)Compares this object to the specified integer object to determine their relative order.static Integerdecode(String string)Parses the specified string and returns aIntegerinstance if the string can be decoded into an integer value.doubledoubleValue()Returns this object's value as a double.booleanequals(Object o)Compares this instance with the specified object and indicates if they are equal.floatfloatValue()Returns this object's value as a float.static IntegergetInteger(String string)Returns theIntegervalue of the system property identified bystring.static IntegergetInteger(String string, int defaultValue)Returns theIntegervalue of the system property identified bystring.static IntegergetInteger(String string, Integer defaultValue)Returns theIntegervalue of the system property identified bystring.inthashCode()Returns an integer hash code for this object.static inthighestOneBit(int i)Determines the highest (leftmost) bit of the specified integer that is 1 and returns the bit mask value for that bit.intintValue()Gets the primitive value of this int.longlongValue()Returns this object's value as a long.static intlowestOneBit(int i)Determines the lowest (rightmost) bit of the specified integer that is 1 and returns the bit mask value for that bit.static intnumberOfLeadingZeros(int i)Determines the number of leading zeros in the specified integer prior to thehighest one bit.static intnumberOfTrailingZeros(int i)Determines the number of trailing zeros in the specified integer after thelowest one bit.static intparseInt(String string)Parses the specified string as a signed decimal integer value.static intparseInt(String string, int radix)Parses the specified string as a signed integer value using the specified radix.static intreverse(int i)Reverses the order of the bits of the specified integer.static intreverseBytes(int i)Reverses the order of the bytes of the specified integer.static introtateLeft(int i, int distance)Rotates the bits of the specified integer to the left by the specified number of bits.static introtateRight(int i, int distance)Rotates the bits of the specified integer to the right by the specified number of bits.shortshortValue()Returns this object's value as a short.static intsignum(int i)Returns the value of thesignumfunction for the specified integer.static StringtoBinaryString(int i)Converts the specified integer into its binary string representation.static StringtoHexString(int i)Converts the specified integer into its hexadecimal string representation.static StringtoOctalString(int i)Converts the specified integer into its octal string representation.StringtoString()Returns a string containing a concise, human-readable description of this object.static StringtoString(int i)Converts the specified integer into its decimal string representation.static StringtoString(int i, int radix)Converts the specified signed integer into a string representation based on the specified radix.static IntegervalueOf(int i)Returns aIntegerinstance for the specified integer value.static IntegervalueOf(String string)Parses the specified string as a signed decimal integer value.static IntegervalueOf(String string, int radix)Parses the specified string as a signed integer value using the specified radix.
-
Field Details
-
MAX_VALUE
public static final int MAX_VALUEConstant for the maximumintvalue, 231-1.- See Also:
- Constant Field Values
-
MIN_VALUE
public static final int MIN_VALUEConstant for the minimumintvalue, -231.- See Also:
- Constant Field Values
-
SIZE
public static final int SIZEConstant for the number of bits needed to represent anintin two's complement form.- Since:
- 1.5
- See Also:
- Constant Field Values
-
TYPE
TheClassobject that represents the primitive typeint.
-
-
Constructor Details
-
Integer
public Integer(int value)Constructs a newIntegerwith the specified primitive integer value.- Parameters:
value- the primitive integer value to store in the new instance.
-
Integer
Constructs a newIntegerfrom the specified string.- Parameters:
string- the string representation of an integer value.- Throws:
NumberFormatException- ifstringcannot be parsed as an integer value.- See Also:
parseInt(String)
-
-
Method Details
-
byteValue
public byte byteValue()Description copied from class:NumberReturns this object's value as a byte. Might involve rounding and/or truncating the value, so it fits into a byte. -
compareTo
Compares this object to the specified integer object to determine their relative order.- Specified by:
compareToin interfaceComparable<Integer>- Parameters:
object- the integer object to compare this object to.- Returns:
- 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 ofobjectare equal; a positive value if the value of this integer is greater than the value ofobject. - Since:
- 1.2
- See Also:
Comparable
-
compare
public static int compare(int lhs, int rhs)Compares twointvalues.- Returns:
- 0 if lhs = rhs, less than 0 if lhs < rhs, and greater than 0 if lhs > rhs.
- Since:
- 1.7
-
decode
Parses the specified string and returns aIntegerinstance 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.- Parameters:
string- a string representation of an integer value.- Returns:
- an
Integercontaining the value represented bystring. - Throws:
NumberFormatException- ifstringcannot be parsed as an integer value.
-
doubleValue
public double doubleValue()Description copied from class:NumberReturns this object's value as a double. Might involve rounding.- Specified by:
doubleValuein classNumber- Returns:
- the primitive double value of this object.
-
equals
Compares this instance with the specified object and indicates if they are equal. In order to be equal,omust be an instance ofIntegerand have the same integer value as this object.- Overrides:
equalsin classObject- Parameters:
o- the object to compare this integer with.- Returns:
trueif the specified object is equal to thisInteger;falseotherwise.- See Also:
Object.hashCode()
-
floatValue
public float floatValue()Description copied from class:NumberReturns this object's value as a float. Might involve rounding.- Specified by:
floatValuein classNumber- Returns:
- the primitive float value of this object.
-
getInteger
Returns theIntegervalue of the system property identified bystring. Returnsnullifstringisnullor empty, if the property can not be found or if its value can not be parsed as an integer.- Parameters:
string- the name of the requested system property.- Returns:
- the requested property's value as an
Integerornull.
-
getInteger
Returns theIntegervalue of the system property identified bystring. Returns the specified default value ifstringisnullor empty, if the property can not be found or if its value can not be parsed as an integer.- Parameters:
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.- Returns:
- the requested property's value as an
Integeror the default value.
-
getInteger
Returns theIntegervalue of the system property identified bystring. Returns the specified default value ifstringisnullor empty, if the property can not be found or if its value can not be parsed as an integer.- Parameters:
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.- Returns:
- the requested property's value as an
Integeror the default value.
-
hashCode
public int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually 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
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
intValue
public int intValue()Gets the primitive value of this int. -
longValue
public long longValue()Description copied from class:NumberReturns this object's value as a long. Might involve rounding and/or truncating the value, so it fits into a long. -
parseInt
Parses the specified string as a signed decimal integer value. The ASCII character - ('-') is recognized as the minus sign.- Parameters:
string- the string representation of an integer value.- Returns:
- the primitive integer value represented by
string. - Throws:
NumberFormatException- ifstringcannot be parsed as an integer value.
-
parseInt
Parses the specified string as a signed integer value using the specified radix. The ASCII character - ('-') is recognized as the minus sign.- Parameters:
string- the string representation of an integer value.radix- the radix to use when parsing.- Returns:
- the primitive integer value represented by
stringusingradix. - Throws:
NumberFormatException- ifstringcannot be parsed as an integer value, orradix < Character.MIN_RADIX || radix > Character.MAX_RADIX.
-
shortValue
public short shortValue()Description copied from class:NumberReturns this object's value as a short. Might involve rounding and/or truncating the value, so it fits into a short.- Overrides:
shortValuein classNumber- Returns:
- the primitive short value of this object.
-
toBinaryString
Converts the specified integer into its binary string representation. The returned string is a concatenation of '0' and '1' characters.- Parameters:
i- the integer to convert.- Returns:
- the binary string representation of
i.
-
toHexString
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- the integer to convert.- Returns:
- the hexadecimal string representation of
i.
-
toOctalString
Converts the specified integer into its octal string representation. The returned string is a concatenation of characters from '0' to '7'.- Parameters:
i- the integer to convert.- Returns:
- the octal string representation of
i.
-
toString
Description copied from class:ObjectReturns 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
toStringmethod if you intend implementing your owntoStringmethod. -
toString
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- the integer to convert.- Returns:
- the decimal string representation of
i.
-
toString
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. Ifradixis not in the interval defined byCharacter.MIN_RADIXandCharacter.MAX_RADIXthen 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, ortoOctalString(int)more convenient.- Parameters:
i- the signed integer to convert.radix- the base to use for the conversion.- Returns:
- the string representation of
i.
-
valueOf
Parses the specified string as a signed decimal integer value.- Parameters:
string- the string representation of an integer value.- Returns:
- an
Integerinstance containing the integer value represented bystring. - Throws:
NumberFormatException- ifstringcannot be parsed as an integer value.- See Also:
parseInt(String)
-
valueOf
Parses the specified string as a signed integer value using the specified radix.- Parameters:
string- the string representation of an integer value.radix- the radix to use when parsing.- Returns:
- an
Integerinstance containing the integer value represented bystringusingradix. - Throws:
NumberFormatException- ifstringcannot be parsed as an integer value, orradix < Character.MIN_RADIX || radix > Character.MAX_RADIX.- See Also:
parseInt(String, int)
-
highestOneBit
public 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. This is also referred to as the Most Significant 1 Bit. Returns zero if the specified integer is zero.- Parameters:
i- the integer to examine.- Returns:
- the bit mask indicating the highest 1 bit in
i. - Since:
- 1.5
-
lowestOneBit
public 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. This is also referred to as the Least Significant 1 Bit. Returns zero if the specified integer is zero.- Parameters:
i- the integer to examine.- Returns:
- the bit mask indicating the lowest 1 bit in
i. - Since:
- 1.5
-
numberOfLeadingZeros
public static int numberOfLeadingZeros(int i)Determines the number of leading zeros in the specified integer prior to thehighest one bit.- Parameters:
i- the integer to examine.- Returns:
- the number of leading zeros in
i. - Since:
- 1.5
-
numberOfTrailingZeros
public static int numberOfTrailingZeros(int i)Determines the number of trailing zeros in the specified integer after thelowest one bit.- Parameters:
i- the integer to examine.- Returns:
- the number of trailing zeros in
i. - Since:
- 1.5
-
bitCount
public static int bitCount(int i)Counts the number of 1 bits in the specified integer; this is also referred to as population count.- Parameters:
i- the integer to examine.- Returns:
- the number of 1 bits in
i. - Since:
- 1.5
-
rotateLeft
public static int rotateLeft(int i, int distance)Rotates the bits of the specified integer to the left by the specified number of bits.- Parameters:
i- the integer value to rotate left.distance- the number of bits to rotate.- Returns:
- the rotated value.
- Since:
- 1.5
-
rotateRight
public static int rotateRight(int i, int distance)Rotates the bits of the specified integer to the right by the specified number of bits.- Parameters:
i- the integer value to rotate right.distance- the number of bits to rotate.- Returns:
- the rotated value.
- Since:
- 1.5
-
reverseBytes
public static int reverseBytes(int i)Reverses the order of the bytes of the specified integer.- Parameters:
i- the integer value for which to reverse the byte order.- Returns:
- the reversed value.
- Since:
- 1.5
-
reverse
public static int reverse(int i)Reverses the order of the bits of the specified integer.- Parameters:
i- the integer value for which to reverse the bit order.- Returns:
- the reversed value.
- Since:
- 1.5
-
signum
public static int signum(int i)Returns the value of thesignumfunction for the specified integer.- Parameters:
i- the integer value to check.- Returns:
- -1 if
iis negative, 1 ifiis positive, 0 ifiis zero. - Since:
- 1.5
-
valueOf
Returns aIntegerinstance for the specified integer value.If it is not necessary to get a new
Integerinstance, 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- the integer value to store in the instance.- Returns:
- a
Integerinstance containingi. - Since:
- 1.5
-