Top Description Inners Fields Constructors Methods
java.lang

public final Class Integer

extends Number
implements Comparable<Integer>, Constable, ConstantDesc
Class Inheritance
All Implemented Interfaces
java.lang.constant.ConstantDesc, java.lang.constant.Constable, java.lang.Comparable
Annotations
@ValueBased
Imports
jdk.internal.misc.CDS, .VM, jdk.internal.vm.annotation.ForceInline, .IntrinsicCandidate, .Stable, java.lang.annotation.Native, java.lang.constant.Constable, .ConstantDesc, java.lang.invoke.MethodHandles, java.util.Objects, .Optional

The Integer class wraps a value of the primitive type 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.

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Implementation Note

The implementations of the "bit twiddling" methods (such as highestOneBit and numberOfTrailingZeros) are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).

Authors
Lee Boynton, Arthur van Hoff, Josh Bloch, Joseph D. Darcy
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Integer.IntegerCache

Cache to support the object identity semantics of autoboxing for values between -128 and 127 (inclusive) as required by JLS.

Field Summary

Modifier and TypeField and Description
public static final int
BYTES

The number of bytes used to represent an int value in two's complement binary form.

pack-priv static final char[]
digits

All possible chars for representing a number as a String

public static final int
MAX_VALUE

A constant holding the maximum value an int can have, 231-1.

public static final int
MIN_VALUE

A constant holding the minimum value an int can have, -231.

private static final long
serialVersionUID

Hides java.lang.Number.serialVersionUID.

use serialVersionUID from JDK 1.0.2 for interoperability
public static final int
SIZE

The number of bits used to represent an int value in two's complement binary form.

public static final Class<Integer>
TYPE

The Class instance representing the primitive type int.

private final int
value

The value of the Integer.

Constructor Summary

AccessConstructor and Description
public
Integer(int
the value to be represented by the Integer object.
value
)

Deprecated for removal since 9. It is rarely appropriate to use this constructor.
Constructs a newly allocated Integer object that represents the specified int value.
public
Integer(String
the String to be converted to an Integer.
s
)

Deprecated for removal since 9. It is rarely appropriate to use this constructor.
Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the number of one-bits in the two's complement binary representation of the specified int value.
bitCount
(int
the value whose bits are to be counted
i
)

Returns the number of one-bits in the two's complement binary representation of the specified int value.

public byte
byteValue()

Overrides java.lang.Number.byteValue.

Returns the value of this Integer as a byte after a narrowing primitive conversion.
public static int

Returns:

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
compare
(int
the first int to compare
x
,
int
the second int to compare
y
)

Compares two int values numerically.

public int

Returns:

the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Integer is numerically greater than the argument Integer (signed comparison).
compareTo
(Integer
the Integer to be compared.
anotherInteger
)

Implements java.lang.Comparable.compareTo.

Compares two Integer objects numerically.
public static int

Returns:

the value 0 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 values
compareUnsigned
(int
the first int to compare
x
,
int
the second int to compare
y
)

Compares two int values numerically treating the values as unsigned.

public static int

Returns:

the compressed value
compress
(int
the value whose bits are to be compressed
i
,
int
the bit mask
mask
)

Returns the value obtained by compressing the bits of the specified int value, i, in accordance with the specified bit mask.

public static Integer

Returns:

an Integer object holding the int value represented by nm
decode
(String
the String to decode.
nm
)

Decodes a String into an Integer.

public Optional<Integer>

Returns:

an Optional describing the Integer instance
describeConstable
()

Implements java.lang.constant.Constable.describeConstable.

Returns an Optional containing the nominal descriptor for this instance, which is the instance itself.
public static int

Returns:

the unsigned quotient of the first argument divided by the second argument
divideUnsigned
(int
the value to be divided
dividend
,
int
the value doing the dividing
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.

public double
doubleValue()

Implements abstract java.lang.Number.doubleValue.

Returns the value of this Integer as a double after a widening primitive conversion.
public boolean

Returns:

true if the objects are the same; false otherwise.
equals
(Object
the object to compare with.
obj
)

Overrides java.lang.Object.equals.

Compares this object to the specified object.
public static int

Returns:

the expanded value
expand
(int
the value whose bits are to be expanded
i
,
int
the bit mask
mask
)

Returns the value obtained by expanding the bits of the specified int value, i, in accordance with the specified bit mask.

public float
floatValue()

Implements abstract java.lang.Number.floatValue.

Returns the value of this Integer as a float after a widening primitive conversion.
private static void
formatUnsignedInt(int
the unsigned int to format
val
,
int
the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
shift
,
byte[]
the byte buffer to write to
buf
,
int
the number of characters to write
len
)

Format an int (treated as unsigned) into a byte buffer (LATIN1 version).

private static void
formatUnsignedIntUTF16(int
the unsigned int to format
val
,
int
the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
shift
,
byte[]
the byte buffer to write to
buf
,
int
the number of characters to write
len
)

Format an int (treated as unsigned) into a byte buffer (UTF16 version).

public static Integer

Returns:

the Integer value of the property.
getInteger
(String
property name.
nm
)

Determines the integer value of the system property with the specified name.

public static Integer

Returns:

the Integer value of the property.
getInteger
(String
property name.
nm
,
int
default value.
val
)

Determines the integer value of the system property with the specified name.

public static Integer

Returns:

the Integer value of the property.
getInteger
(String
property name.
nm
,
Integer
default value.
val
)

Returns the integer value of the system property with the specified name.

public int

Returns:

a hash code value for this object, equal to the primitive int value represented by this Integer object.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code for this Integer.
public static int

Returns:

a hash code value for an int value.
hashCode
(int
the value to hash
value
)

Returns a hash code for an int value; compatible with Integer.hashCode().

public static int

Returns:

an int value with a single one-bit, in the position of the highest-order one-bit in the specified value, or zero if the specified value is itself equal to zero.
highestOneBit
(int
the value whose highest one bit is to be computed
i
)

Returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value.

public int
intValue()

Implements abstract java.lang.Number.intValue.

Returns the value of this Integer as an int.
public long
longValue()

Implements abstract java.lang.Number.longValue.

Returns the value of this Integer as a long after a widening primitive conversion.
public static int

Returns:

an int value with a single one-bit, in the position of the lowest-order one-bit in the specified value, or zero if the specified value is itself equal to zero.
lowestOneBit
(int
the value whose lowest one bit is to be computed
i
)

Returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.

public static int

Returns:

the greater of a and b
max
(int
the first operand
a
,
int
the second operand
b
)

Returns the greater of two int values as if by calling Math.max.

public static int

Returns:

the smaller of a and b
min
(int
the first operand
a
,
int
the second operand
b
)

Returns the smaller of two int values as if by calling Math.min.

public static int

Returns:

the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified int value, or 32 if the value is equal to zero.
numberOfLeadingZeros
(int
the value whose number of leading zeros is to be computed
i
)

Returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified int value.

public static int

Returns:

the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value, or 32 if the value is equal to zero.
numberOfTrailingZeros
(int
the value whose number of trailing zeros is to be computed
i
)

Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value.

private static int
parallelSuffix(int maskCount)

public static int

Returns:

the integer represented by the string argument in the specified radix.
parseInt
(String
the String containing the integer representation to be parsed
s
,
int
the radix to be used while parsing s.
radix
)

Parses the string argument as a signed integer in the radix specified by the second argument.

public static int

Returns:

the signed int represented by the subsequence in the specified radix.
parseInt
(CharSequence
the CharSequence containing the int representation to be parsed
s
,
int
the beginning index, inclusive.
beginIndex
,
int
the ending index, exclusive.
endIndex
,
int
the radix to be used while parsing s.
radix
)

Parses the CharSequence argument as a signed int in the specified radix, beginning at the specified beginIndex and extending to endIndex - 1.

public static int

Returns:

the integer value represented by the argument in decimal.
parseInt
(String
a String containing the int representation to be parsed
s
)

Parses the string argument as a signed decimal integer.

public static int

Returns:

the integer represented by the string argument in the specified radix.
parseUnsignedInt
(String
the String containing the unsigned integer representation to be parsed
s
,
int
the radix to be used while parsing s.
radix
)

Parses the string argument as an unsigned integer in the radix specified by the second argument.

public static int

Returns:

the unsigned int represented by the subsequence in the specified radix.
parseUnsignedInt
(CharSequence
the CharSequence containing the unsigned int representation to be parsed
s
,
int
the beginning index, inclusive.
beginIndex
,
int
the ending index, exclusive.
endIndex
,
int
the radix to be used while parsing s.
radix
)

Parses the CharSequence argument as an unsigned int in the specified radix, beginning at the specified beginIndex and extending to endIndex - 1.

public static int

Returns:

the unsigned integer value represented by the argument in decimal.
parseUnsignedInt
(String
a String containing the unsigned int representation to be parsed
s
)

Parses the string argument as an unsigned decimal integer.

public static int

Returns:

the unsigned remainder of the first argument divided by the second argument
remainderUnsigned
(int
the value to be divided
dividend
,
int
the value doing the dividing
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.

public Integer

Returns:

the Integer instance
resolveConstantDesc
(MethodHandles.Lookup
ignored
lookup
)

Implements java.lang.constant.ConstantDesc.resolveConstantDesc.

Resolves this instance as a ConstantDesc, the result of which is the instance itself.
public static int

Returns:

the value obtained by reversing order of the bits in the specified int value.
reverse
(int
the value to be reversed
i
)

Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.

public static int

Returns:

the value obtained by reversing the bytes in the specified int value.
reverseBytes
(int
the value whose bytes are to be reversed
i
)

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified int value.

public static int

Returns:

the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits.
rotateLeft
(int
the value whose bits are to be rotated left
i
,
int
the number of bit positions to rotate left
distance
)

Returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits.

public static int

Returns:

the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits.
rotateRight
(int
the value whose bits are to be rotated right
i
,
int
the number of bit positions to rotate right
distance
)

Returns the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits.

public short
shortValue()

Overrides java.lang.Number.shortValue.

Returns the value of this Integer as a short after a narrowing primitive conversion.
public static int

Returns:

the signum function of the specified int value.
signum
(int
the value whose signum is to be computed
i
)

Returns the signum function of the specified int value.

pack-priv static int

Returns:

string size
stringSize
(int
int value
x
)

Returns the string representation size for a given int value.

public static int

Returns:

the sum of a and b
sum
(int
the first operand
a
,
int
the second operand
b
)

Adds two integers together as per the + operator.

public static String

Returns:

the string representation of the unsigned integer value represented by the argument in binary (base 2).
toBinaryString
(int
an integer to be converted to a string.
i
)

Returns a string representation of the integer argument as an unsigned integer in base 2.

public static String

Returns:

the string representation of the unsigned integer value represented by the argument in hexadecimal (base 16).
toHexString
(int
an integer to be converted to a string.
i
)

Returns a string representation of the integer argument as an unsigned integer in base 16.

public static String

Returns:

the string representation of the unsigned integer value represented by the argument in octal (base 8).
toOctalString
(int
an integer to be converted to a string.
i
)

Returns a string representation of the integer argument as an unsigned integer in base 8.

public static String

Returns:

a string representation of the argument in the specified radix.
toString
(int
an integer to be converted to a string.
i
,
int
the radix to use in the string representation.
radix
)

Returns a string representation of the first argument in the radix specified by the second argument.

public static String

Returns:

a string representation of the argument in base 10.
toString
(int
an integer to be converted.
i
)

Returns a String object representing the specified integer.

public String

Returns:

a string representation of the value of this object in base 10.
toString
()

Overrides java.lang.Object.toString.

Returns a String object representing this Integer's value.
private static String
toStringUTF16(int i, int radix)

public static long

Returns:

the argument converted to long by an unsigned conversion
toUnsignedLong
(int
the value to convert to an unsigned long
x
)

Converts the argument to a long by an unsigned conversion.

public static String

Returns:

an unsigned string representation of the argument in the specified radix.
toUnsignedString
(int
an integer to be converted to an unsigned string.
i
,
int
the radix to use in the string representation.
radix
)

Returns a string representation of the first argument as an unsigned integer value in the radix specified by the second argument.

public static String

Returns:

an unsigned string representation of the argument.
toUnsignedString
(int
an integer to be converted to an unsigned string.
i
)

Returns a string representation of the argument as an unsigned decimal value.

private static String
toUnsignedString0(int val, int shift)

Convert the integer to an unsigned number.

public static Integer

Returns:

an Integer object holding the value represented by the string argument in the specified radix.
valueOf
(String
the string to be parsed.
s
,
int
the radix to be used in interpreting s
radix
)

Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.

public static Integer

Returns:

an Integer object holding the value represented by the string argument.
valueOf
(String
the string to be parsed.
s
)

Returns an Integer object holding the value of the specified String.

public static Integer

Returns:

an Integer instance representing i.
valueOf
(int
an int value.
i
)

Returns an Integer instance representing the specified int value.