public final class Integer extends Number implements Comparable<Integer>
Integer
represent primitive
int
values.
Additionally, this class provides various helper functions and variables
related to ints.Modifier and Type | Field and Description |
---|---|
static int |
MAX_VALUE
The maximum value an
int can represent is 2147483647 (or
231 - 1). |
static int |
MIN_VALUE
The minimum value an
int can represent is -2147483648 (or
-231). |
static int |
SIZE
The number of bits needed to represent an
int . |
static Class<Integer> |
TYPE
The primitive type
int is represented by this
Class object. |
Constructor and Description |
---|
Integer(int value)
Create an
Integer object representing the value of the
int argument. |
Integer(String s)
Create an
Integer object representing the value of the
argument after conversion to an int . |
Modifier and Type | Method and Description |
---|---|
static int |
bitCount(int x)
Return the number of bits set in x.
|
byte |
byteValue()
Return the value of this
Integer as a byte . |
int |
compareTo(Integer i)
Compare two Integers numerically by comparing their
int
values. |
static Integer |
decode(String str)
Convert the specified
String into an Integer . |
double |
doubleValue()
Return the value of this
Integer as a double . |
boolean |
equals(Object obj)
Returns
true if obj is an instance of
Integer and represents the same int value. |
float |
floatValue()
Return the value of this
Integer as a float . |
static Integer |
getInteger(String nm)
Get the specified system property as an
Integer . |
static Integer |
getInteger(String nm,
int val)
Get the specified system property as an
Integer , or use a
default int value if the property is not found or is not
decodable. |
static Integer |
getInteger(String nm,
Integer def)
Get the specified system property as an
Integer , or use a
default Integer value if the property is not found or is
not decodable. |
int |
hashCode()
Return a hashcode representing this Object.
|
static int |
highestOneBit(int value)
Find the highest set bit in value, and return a new value
with only that bit set.
|
int |
intValue()
Return the value of this
Integer . |
long |
longValue()
Return the value of this
Integer as a long . |
static int |
lowestOneBit(int value)
Find the lowest set bit in value, and return a new value
with only that bit set.
|
static int |
numberOfLeadingZeros(int value)
Return the number of leading zeros in value.
|
static int |
numberOfTrailingZeros(int value)
Find the number of trailing zeros in value.
|
static int |
parseInt(String s)
Converts the specified
String into an int . |
static int |
parseInt(String str,
int radix)
Converts the specified
String into an int
using the specified radix (base). |
static int |
reverse(int val)
Reverse the bits in val.
|
static int |
reverseBytes(int val)
Reverse the bytes in val.
|
static int |
rotateLeft(int x,
int distance)
Rotate x to the left by distance bits.
|
static int |
rotateRight(int x,
int distance)
Rotate x to the right by distance bits.
|
short |
shortValue()
Return the value of this
Integer as a short . |
static int |
signum(int x)
Return 1 if x is positive, -1 if it is negative, and 0 if it is
zero.
|
static String |
toBinaryString(int i)
Converts the
int to a String assuming it is
unsigned in base 2. |
static String |
toHexString(int i)
Converts the
int to a String assuming it is
unsigned in base 16. |
static String |
toOctalString(int i)
Converts the
int to a String assuming it is
unsigned in base 8. |
String |
toString()
Converts the
Integer value to a String and
assumes a radix of 10. |
static String |
toString(int i)
Converts the
int to a String and assumes
a radix of 10. |
static String |
toString(int num,
int radix)
Converts the
int to a String using
the specified radix (base). |
static Integer |
valueOf(int val)
Returns an
Integer object wrapping the value. |
static Integer |
valueOf(String s)
Creates a new
Integer object using the String ,
assuming a radix of 10. |
static Integer |
valueOf(String s,
int radix)
Creates a new
Integer object using the String
and specified radix (base). |
public static final int MIN_VALUE
int
can represent is -2147483648 (or
-231).public static final int MAX_VALUE
int
can represent is 2147483647 (or
231 - 1).public static final Class<Integer> TYPE
int
is represented by this
Class
object.public static final int SIZE
int
.public Integer(int value)
Integer
object representing the value of the
int
argument.value
- the value to usepublic Integer(String s)
Integer
object representing the value of the
argument after conversion to an int
.s
- the string to convertNumberFormatException
- if the String does not contain an intvalueOf(String)
public static String toString(int num, int radix)
int
to a String
using
the specified radix (base). If the radix exceeds
Character.MIN_RADIX
or Character.MAX_RADIX
, 10
is used instead. If the result is negative, the leading character is
'-' ('\\u002D'). The remaining characters come from
Character.forDigit(digit, radix)
('0'-'9','a'-'z').num
- the int
to convert to String
radix
- the radix (base) to use in the conversionString
representation of the argumentpublic static String toHexString(int i)
int
to a String
assuming it is
unsigned in base 16.i
- the int
to convert to String
String
representation of the argumentpublic static String toOctalString(int i)
int
to a String
assuming it is
unsigned in base 8.i
- the int
to convert to String
String
representation of the argumentpublic static String toBinaryString(int i)
int
to a String
assuming it is
unsigned in base 2.i
- the int
to convert to String
String
representation of the argumentpublic static String toString(int i)
int
to a String
and assumes
a radix of 10.i
- the int
to convert to String
String
representation of the argumenttoString(int, int)
public static int parseInt(String str, int radix)
String
into an int
using the specified radix (base). The string must not be null
or empty. It may begin with an optional '-', which will negate the answer,
provided that there are also valid digits. Each digit is parsed as if by
Character.digit(d, radix)
, and must be in the range
0
to radix - 1
. Finally, the result must be
within MIN_VALUE
to MAX_VALUE
, inclusive.
Unlike Double.parseDouble, you may not have a leading '+'.str
- the String
to convertradix
- the radix (base) to use in the conversionString
argument converted to int
NumberFormatException
- if s
cannot be parsed as an
int
public static int parseInt(String s)
String
into an int
.
This function assumes a radix of 10.s
- the String
to convertint
value of s
NumberFormatException
- if s
cannot be parsed as an
int
parseInt(String, int)
public static Integer valueOf(String s, int radix)
Integer
object using the String
and specified radix (base).s
- the String
to convertradix
- the radix (base) to convert withInteger
NumberFormatException
- if s
cannot be parsed as an
int
parseInt(String, int)
public static Integer valueOf(String s)
Integer
object using the String
,
assuming a radix of 10.s
- the String
to convertInteger
NumberFormatException
- if s
cannot be parsed as an
int
Integer(String)
,
parseInt(String)
public static Integer valueOf(int val)
Integer
object wrapping the value.
In contrast to the Integer
constructor, this method
will cache some values. It is used by boxing conversion.val
- the value to wrapInteger
public byte byteValue()
Integer
as a byte
.public short shortValue()
Integer
as a short
.shortValue
in class Number
public int intValue()
Integer
.public long longValue()
Integer
as a long
.public float floatValue()
Integer
as a float
.floatValue
in class Number
public double doubleValue()
Integer
as a double
.doubleValue
in class Number
public String toString()
Integer
value to a String
and
assumes a radix of 10.toString
in class Object
String
representationObject.getClass()
,
Object.hashCode()
,
Class.getName()
,
toHexString(int)
public int hashCode()
Integer
's hash
code is simply its value.hashCode
in class Object
Object.equals(Object)
,
System.identityHashCode(Object)
public boolean equals(Object obj)
true
if obj
is an instance of
Integer
and represents the same int value.equals
in class Object
obj
- the object to compareObject.hashCode()
public static Integer getInteger(String nm)
Integer
. The
decode()
method will be used to interpret the value of
the property.nm
- the name of the system propertyInteger
, or null if the
property is not found or cannot be decodedSecurityException
- if accessing the system property is forbiddenSystem.getProperty(String)
,
decode(String)
public static Integer getInteger(String nm, int val)
Integer
, or use a
default int
value if the property is not found or is not
decodable. The decode()
method will be used to interpret
the value of the property.nm
- the name of the system propertyval
- the default valueSecurityException
- if accessing the system property is forbiddenSystem.getProperty(String)
,
decode(String)
public static Integer getInteger(String nm, Integer def)
Integer
, or use a
default Integer
value if the property is not found or is
not decodable. The decode()
method will be used to
interpret the value of the property.nm
- the name of the system propertydef
- the default valueSecurityException
- if accessing the system property is forbiddenSystem.getProperty(String)
,
decode(String)
public static Integer decode(String str)
String
into an Integer
.
The String
may represent decimal, hexadecimal, or
octal numbers.
The extended BNF grammar is as follows:
DecodableString: ( [Finally, the value must be in the range-
] DecimalNumber ) | ( [-
] (0x
|0X
|#
) HexDigit { HexDigit } ) | ( [-
]0
{ OctalDigit } ) DecimalNumber: DecimalDigit except '0' { DecimalDigit } DecimalDigit: Character.digit(d, 10) has value 0 to 9 OctalDigit: Character.digit(d, 8) has value 0 to 7 DecimalDigit: Character.digit(d, 16) has value 0 to 15
MIN_VALUE
to
MAX_VALUE
, or an exception is thrown.str
- the String
to interpretInteger
NumberFormatException
- if s
cannot be parsed as a
int
NullPointerException
- if s
is nullpublic int compareTo(Integer i)
int
values. The result is positive if the first is greater, negative if the
second is greater, and 0 if the two are equal.compareTo
in interface Comparable<Integer>
i
- the Integer to comparepublic static int bitCount(int x)
x
- value to examinepublic static int rotateLeft(int x, int distance)
x
- the value to rotatedistance
- the number of bits by which to rotatepublic static int rotateRight(int x, int distance)
x
- the value to rotatedistance
- the number of bits by which to rotatepublic static int highestOneBit(int value)
value
- the value to examinepublic static int numberOfLeadingZeros(int value)
value
- the value to examinepublic static int lowestOneBit(int value)
value
- the value to examinepublic static int numberOfTrailingZeros(int value)
value
- the value to examinepublic static int signum(int x)
x
- the value to examinepublic static int reverseBytes(int val)
public static int reverse(int val)