int
in an object. An object of type Integer
contains a single field whose type is int
. In addition, this class provides several methods for converting an int
to a String
and a String
to an int
, as well as other constants and methods useful when dealing with an int
.
Field Detail |
public static final int MIN_VALUE
int
. The constant value of this field is -2147483648.public static final int MAX_VALUE
int
. The constant value of this field is 2147483647.public static final Class TYPE
static final char[] digits
private int value
private static final long serialVersionUID
Constructor Detail |
public Integer(int value)
Integer
object that represents the primitive int
argument.value
- the value to be represented by the Integer
.public Integer(String s)
Integer
object that represents the value represented by the string. The string is converted to an int in exactly the manner used by the parseInt method for radix 10.s
- the String
to be converted to an Integer
.NumberFormatException
- if the String
does not contain a parsable integer.Method Detail |
public static String toString(int i, int radix)
If the radix is smaller than Character.MIN_RADIX
or larger than Character.MAX_RADIX
, then the radix 10
is used instead.
If the first argument is negative, the first element of the result is the ASCII minus character '-'
('\u002d'). If the first argument is not negative, no sign character appears in the result.
The remaining characters of the result represent the magnitude of the first argument. If the magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the magnitude will not be the zero character. The following ASCII characters are used as digits:
These are '\u0030' through '\u0039' and '\u0061' through '\u007a'. If the radix is N, then the first N of these characters are used as radix-N digets in the order shown. Thus, the digits for hexadecimal (radix 16) are 0123456789abcdef. If uppercase letters are desired, the {@link java.lang.String#toUpperCase()} method may be called on the result:0123456789abcdefghijklmnopqrstuvwxyz
Integer.toString(n, 16).toUpperCase()
i
- an integer.radix
- the radix.public static String toHexString(int i)
The unsigned integer value is the argument plus 232 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in hexadecimal (base 16) with no extra leading 0
s. If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The following characters are used as hexadecimal digits:
These are the characters '\u0030' through '\u0039' and 'u\0039' through '\u0066'. If the uppercase letters are desired, the {@link java.lang.String#toUpperCase()} method may be called on the result:0123456789abcdef
Long.toHexString(n).toUpperCase()
i
- an integer.public static String toOctalString(int i)
The unsigned integer value is the argument plus 232 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in octal (base 8) with no extra leading 0
s.
If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The octal digits are:
These are the characters '\u0030' through '\u0037'.01234567
i
- an integerpublic static String toBinaryString(int i)
The unsigned integer value is the argument plus 232if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0
s. If the unsigned magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The characters '0' ('\u0030') and '1' ('\u0031') are used as binary digits.
i
- an integer.private static String toUnsignedString(int i, int shift)
public static String toString(int i)
i
- an integer to be converted.public static int parseInt(String s, int radix)
'-'
('\u002d'
) to indicate a negative value. The resulting integer value is returned. An exception of type NumberFormatException is thrown if any of the following situations occurs:
Examples:
parseInt("0", 10) returns 0 parseInt("473", 10) returns 473 parseInt("-0", 10) returns 0 parseInt("-FF", 16) returns -255 parseInt("1100110", 2) returns 102 parseInt("2147483647", 10) returns 2147483647 parseInt("-2147483648", 10) returns -2147483648 parseInt("2147483648", 10) throws a NumberFormatException parseInt("99", 8) throws a NumberFormatException parseInt("Kona", 10) throws a NumberFormatException parseInt("Kona", 27) returns 411787
s
- the String
containing the integer.radix
- the radix to be used.NumberFormatException
- if the string does not contain a parsable integer.public static int parseInt(String s)
'-'
('\u002d') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the {@link #parseInt(java.lang.String, int)} method.s
- a string.NumberFormatException
- if the string does not contain a parsable integer.public static Integer valueOf(String s, int radix)
Integer
object that represents the integer value specified by the string. In other words, this method returns an Integer
object equal to the value of:
new Integer(Integer.parseInt(s, radix))
s
- the string to be parsed.radix
- the radix of the integer represented by string sInteger
initialized to the value represented by the string argument in the specified radix.NumberFormatException
- if the String cannot be parsed as an int
.public static Integer valueOf(String s)
In other words, this method returns an Integer object equal to the value of:
new Integer(Integer.parseInt(s))
s
- the string to be parsed.Integer
initialized to the value represented by the string argument.NumberFormatException
- if the string cannot be parsed as an integer.public byte byteValue()
public short shortValue()
public int intValue()
int
value represented by this object.public long longValue()
int
value represented by this object that is converted to type long
and the result of the conversion is returned.public float floatValue()
int
value represented by this object is converted to type float
and the result of the conversion is returned.public double doubleValue()
int
value represented by this object is converted to type double
and the result of the conversion is returned.public String toString()
public int hashCode()
public boolean equals(Object obj)
true
if and only if the argument is not null
and is an Integer
object that contains the same int
value as this object.obj
- the object to compare with.true
if the objects are the same; false
otherwise.public static Integer getInteger(String nm)
The first argument is treated as the name of a system property. System properties are accessible through the {@link java.lang.System#getProperty(java.lang.String)} method. The string value of this property is then interpreted as an integer value and an Integer
object representing this value is returned. Details of possible numeric formats can be found with the definition of getProperty
.
If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null
is returned. In other words, this method returns an Integer
object equal to the value of:
getInteger(nm, null)
nm
- property name.Integer
value of the property.public static Integer getInteger(String nm, int val)
The first argument is treated as the name of a system property. System properties are accessible through getProperty
, a method defined by the System
class. The string value of this property is then interpreted as an integer value and an Integer
object representing this value is returned. Details of possible numeric formats can be found with the definition of getProperty
.
The second argument is the default value. An Integer
object that represents the value of the second argument is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.
In other words, this method returns an Integer
object equal to the value of:
but in practice it may be implemented in a manner such as:getInteger(nm, new Integer(val))
to avoid the unnecessary allocation of anInteger result = getInteger(nm, null); return (result == null) ? new Integer(val) : result;
Integer
object when the default value is not needed.nm
- property name.val
- default value.Integer
value of the property.public static Integer getInteger(String nm, Integer val)
getProperty
, a method defined by the System
class. The string value of this property is then interpreted as an integer value, as per the Integer.decode
method, and an Integer
object representing this value is returned.
0x
or the ASCII character #
, not followed by a minus sign, then the rest of it is parsed as a hexadecimal integer exactly as for the method {@link #valueOf(java.lang.String, int)} with radix 16. 0
followed by another character, it is parsed as an octal integer exactly as for the method {@link #valueOf(java.lang.String, int) with radix 8. The second argument is the default value. The default value is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.
nm
- property name.val
- default value.Integer
value of the property.public static Integer decode(String nm)
String
into an Integer
. Accepts decimal, hexadecimal, and octal numbers, in the following formats: [-] decimal constant [-] 0x hex constant [-] # hex constant [-] 0 octal constantThe constant following an (optional) negative sign and/or "radix specifier" is parsed as by the
Integer.parseInt
method with the specified radix (10, 8 or 16). This constant must be positive or a NumberFormatException will result. The result is made negative if first character of the specified String
is the negative sign. No whitespace characters are permitted in the String
.nm
- the String
to decode.Integer
represented by the specified string.NumberFormatException
- if the String
does not contain a parsable integer.public int compareTo(Integer anotherInteger)
anotherInteger
- the Integer
to be compared.0
if the argument Integer is equal to this Integer; a value less than 0
if this Integer is numerically less than the Integer argument; and a value greater than 0
if this Integer is numerically greater than the Integer argument (signed comparison).public int compareTo(Object o)
compareTo(Integer)
. Otherwise, it throws a ClassCastException
(as Integers are comparable only to other Integers).o
- the Object
to be compared.0
if the argument is a Integer numerically equal to this Integer; a value less than 0
if the argument is a Integer numerically greater than this Integer; and a value greater than 0
if the argument is a Integer numerically less than this Integer.ClassCastException
- if the argument is not an Integer
.