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.

Integer a = 100;
Integer b = 100;
System.out.println(a == b); 
 
Integer c = 200;
Integer d = 200;
System.out.println(c == d); 
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>.

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

Integer Fields

FieldDescription
static int MAX_VALUEA constant holding the maximum value an int can have:
static int MIN_VALUEA constant holding the maximum value an int can have:

Integer methods

MethodDescriptionRef
static int comapre(int x, int y)It compares two int values numerically and returns the result as -1, 0 or 1.
int compareTo(Integer another)It compares two integer objects numerically and returns the result as -1, 0 or 1.
boolean equals(Object obj)It compares the value of the parameter to the value of the current Integer object, returns true or false
static int parseInt(String s)It parses a string into primitive integer, string must be decimal digits, except first character being minus (-) for negative value or plus (+) for positive value
static Integer valueOf(int i)It returns the relevant Integer Object holding the value of the argument passed
String toString(int i)It returns a String object representing the value of the parameter integer value

compare()

The compare() method compares two parameter int values x and y. It returns:

  • The value 0, if x equals y  
  • The value less than 0, if x is less than y    
  • The value greater than 0, if x is greater than y

The value returned is identical to what would be returned by: Integer.valueOf(x).compareTo(Integer.valueOf(y))

public class Demo {
	public static void main(String[] args) {
		int num1 = 10;
		int num2 = 20;
		int num3 = 10;
		int num4 = 30;
		
		// num1 < num2
		System.out.println(Integer.compare(num1, num2));
		// num1 == num3
		System.out.println(Integer.compare(num1, num3));
		// num4 > num2
		System.out.println(Integer.compare(num4, num2));
	}
}
-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: