Class relations: + java.lang.Object + - java.lang.Number + - - java.lang.Integer
Implemented interfaces:
Serializable, [[Comparable Interface|Comparable
Thread-safe: True
The Integer
class is the wrapper class for the primitive data type int
. The Integer
class constructor can take a primitive integer or a string representation of an integer. The Integer
object in Java is immutable, once it is created it cannot be changed, this makes Integer
inherently thread-safe. The Integer
class is compatible with the Java Autoboxing and Unboxing mechanism.
In Java, the range of value for integer from -128 to 128 instantiation results in cached instances. This is because Java caches integer objects within this range for performance reasons. When you instantiate an integer object within this range, it may return a reference to an existing object rather than creating a new one. However, for values outside this range, new integer objects are always created.
true
false
ValueOf()
The integer class valueOf()
method is used to return an integer instance. It is open sourced, and you can discover the operation to retrieve integer reference from the cache if the value of integer to return falls between lower cache bound to higher cache bound, which is -128 to 128 by default, you can control the bound with the system property java.lang.Integer.IntegerCache.high = <new_higer_bound>
.
Integer Fields
Field | Description |
---|---|
static int MAX_VALUE | A constant holding the maximum value an int can have: |
static int MIN_VALUE | A constant holding the maximum value an int can have: |
Integer methods
compare()
The compare()
method compares two parameter int
values x
and y
.
It returns:
- The value 0, if
x
equalsy
- The value less than 0, if
x
is less thany
- The value greater than 0, if
x
is greater thany
The value returned is identical to what would be returned by:
Integer.valueOf(x).compareTo(Integer.valueOf(y))
-1
0
1
compareTo()
The compareTo()
method compares the calling integer value with the parameter integer value.
It returns:
- The value 0, if this integer equals the parameter integer
- The value less than 0, if this integer is less than the parameter integer
- The value greater than 0, if this integer is greater than the parameter integer
Back to parent page: Java Standard Edition (Java SE) and Java Programming
Web_and_App_DevelopmentProgramming_LanguagesJavaIntegerWrapper_Class
Reference: