Of course! Let's dive deep into java.lang.Integer. It's one of the most fundamental and widely used classes in Java.

What is java.lang.Integer?
In short, java.lang.Integer is a wrapper class in Java. It wraps a value of the primitive data type int in an object. This allows you to use an int where an object is required, such as in collections (like ArrayList or HashMap) or in generics.
Think of it this way:
intis the primitive: It's a simple value, like10. It's not an object, it's just a number stored directly in memory.Integeris the object: It's an "instance" or a "box" that contains anintvalue. You can call methods on it, and it can benull.
Key Characteristics and Features
The int Primitive vs. The Integer Object
| Feature | int (Primitive) |
Integer (Object) |
|---|---|---|
| Type | Primitive data type | Wrapper class (java.lang.Integer) |
| Default Value | 0 |
null |
Can be null? |
No | Yes |
| Memory Usage | Small, fixed size (typically 4 bytes) | Larger, has object overhead (e.g., header, reference) |
| Usage | For arithmetic operations, performance-critical code. | When an object is needed (collections, generics, APIs). |
| Example | int score = 100; |
Integer score = new Integer(100); (or Integer score = 100;) |
The Autoboxing and Unboxing Magic
Since Java 5, the compiler automatically handles the conversion between int and Integer. This is called autoboxing and unboxing.
-
Autoboxing: Converting an
intto anInteger.
(图片来源网络,侵删)// Before Java 5 (manual boxing) Integer number = new Integer(42); // After Java 5 (autoboxing) Integer number = 42; // The compiler does the conversion for you!
-
Unboxing: Converting an
Integerto anint.Integer myInteger = 100; // Before Java 5 (manual unboxing) int myInt = myInteger.intValue(); // After Java 5 (unboxing) int myInt = myInteger; // The compiler does the conversion for you!
This makes the code much cleaner and less error-prone. It also allows you to use null values, which can be useful.
Integer value = null; // This is perfectly valid // int result = value; // This would throw a NullPointerException at runtime!
The Integer Cache (A Crucial Detail)
For performance and memory efficiency, Java caches Integer objects for values between -128 and 127. This is known as the IntegerCache.
Integer a = 100; Integer b = 100; System.out.println(a == b); // Output: true // Why? 'a' and 'b' refer to the SAME cached object in memory. Integer c = 200; Integer d = 200; System.out.println(c == d); // Output: false // Why? 200 is outside the cached range (-128 to 127), so 'c' and 'd' // are two different objects in memory.
The operator compares object references (memory addresses), not values. For comparing Integer objects for equality of their values, you should always use .equals().

Integer c = 200; Integer d = 200; System.out.println(c.equals(d)); // Output: true (Correct way to compare values)
Useful Static Methods
The Integer class provides many static utility methods.
-
parseInt(String s): Parses a string into anint.int num = Integer.parseInt("123"); // num is now 123 // int badNum = Integer.parseInt("abc"); // Throws NumberFormatException -
valueOf(String s): Parses a string into anIntegerobject.Integer numObj = Integer.valueOf("456"); // numObj is an Integer object -
toString(int i): Converts anintto aString.String str = Integer.toString(789); // str is "789"
-
toBinaryString(int i),toHexString(int i),toOctalString(int i): Convert anintto a string representation in a different base.System.out.println(Integer.toBinaryString(10)); // Output: 1010 System.out.println(Integer.toHexString(255)); // Output: ff
-
max(int a, int b)andmin(int a, int b): Return the larger or smaller of twointvalues.int bigger = Integer.max(10, 20); // bigger is 20 int smaller = Integer.min(10, 20); // smaller is 10
Constant Fields
The class also defines useful constants:
Integer.MAX_VALUE: The maximum value anintcan hold, which is2,147,483,647.Integer.MIN_VALUE: The minimum value anintcan hold, which is-2,147,483,648.Integer.SIZE: The number of bits used to represent anintvalue, which is32.Integer.BYTES: The number of bytes used to represent anintvalue, which is4.
Practical Use Cases
Using Collections
You cannot use primitive types like int in collections that require objects.
// This will NOT compile // List<int> numbers = new ArrayList<>(); // This is the correct way List<Integer> numbers = new ArrayList<>(); numbers.add(10); // Autoboxing occurs here (10 -> Integer(10)) numbers.add(20); numbers.add(null); // An Integer list can hold null values! int first = numbers.get(0); // Unboxing occurs here (Integer(10) -> 10)
Handling Null Values
Sometimes, a value might be missing. An int cannot represent "missing," but an Integer can be null.
public class UserProfile {
private Integer age; // Age can be unknown (null)
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Generics
Generics in Java can only work with object types, not primitives. Integer is essential for using generics with numbers.
// A generic class to hold a number
class Box<T> {
private T value;
// ...
}
// You must use Integer, not int
Box<Integer> intBox = new Box<>();
intBox.setValue(100);
Summary
| Topic | Key Takeaway |
|---|---|
| Purpose | A wrapper class for the primitive int type, allowing it to be used as an object. |
| Autoboxing/Unboxing | Automatic conversion between int and Integer since Java 5, simplifying code. |
| Integer Cache | Values from -128 to 127 are cached. Integer a = 100; Integer b = 100; will be a == b. |
| Equality | Always use .equals() to compare Integer values. compares object references. |
| Nullability | Integer can be null; int cannot. This is critical for representing missing data. |
| Statics | Provides essential methods like parseInt(), valueOf(), toString(), MAX_VALUE, etc. |
| Usage | Essential for Java Collections, Generics, and APIs that require objects. |
