• The basic types in Java are not objects and does not inherit from Object.

  • Since Java 1.5 introduced allowed auto boxing between int and Integer(and the other types).

  • Because ints aren't Objects that can't be used as generic type parameters eg the T in list<T>

Answer from mikek3332002 on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... The Integer class wraps a value of the primitive type int in an object.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-integer-class-java
Java.lang.Integer class in Java - GeeksforGeeks
June 21, 2022 - Java Collection · Last Updated ... like converting it to a string representation, and vice-versa. An object of the Integer class can hold a single int value....
🌐
Scaler
scaler.com › home › topics › integer class in java
Integer Class in Java| Scaler Topics
April 14, 2024 - The java.lang.Integer class creates an object out of an int value. An object of type Integer has a single field with the type int.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... The Integer class wraps a value of the primitive type int in an object.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › java_lang_integer.htm
Java - Integer class
The Java Integer class wraps a value of primitive type int in an object. An object of type Integer contains a single field whose type is int. Following is the declaration for java.lang.Integer class − Following are the fields for java.lang.Integer
🌐
MIT
web.mit.edu › java_v1.0.2 › www › javadoc › java.lang.Integer.html
Class java.lang.Integer
Assuming the specified String represents an integer, returns that integer's value. ... Returns a new String object representing the specified integer as unsigned binary number.
🌐
CodeGym
codegym.cc › java blog › java classes › java.lang.integer class
Java.lang.Integer Class
February 20, 2025 - As a wrapper class, Integer provides various methods for working with int, as well as a number of methods for converting int to String and String to int. The class has two constructors: public Integer(int i), where i is a primitive value to ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › create-an-integer-object-in-java
Create an Integer object in Java
December 18, 2024 - You can create an Integer object using the Integer constructor by passing a primitive int value. In Java, a constructor is a special method whose name is exactly the same as the class name.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 21 & JDK 21)
January 20, 2026 - Returns a string representation of the integer argument as an unsigned integer in base 8. ... Returns a String object representing this Integer's value.
🌐
University of Washington
courses.cs.washington.edu › courses › cse341 › 98au › java › jdk1.2beta4 › docs › api › java › lang › Integer.html
Class java.lang.Integer
Constructs a newly allocated Integer object that represents the value represented by the string. The string is converted to an int value as if by the valueOf method. Parameters: s - the String to be converted to an Integer. Throws: NumberFormatException - if the String does not contain a parsable ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.integer
Integer Class (Java.Lang) | Microsoft Learn
The Integer class wraps a value of the primitive type int in an object. [Android.Runtime.Register("java/lang/Integer", DoNotGenerateAcw=true)] public sealed class Integer : Java.Lang.Number, IConvertible, IDisposable, Java.Interop.IJavaPeerable, ...
🌐
Oracle
docs.oracle.com › en › java › javase › 20 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 20 & JDK 20)
July 10, 2023 - Returns a string representation of the integer argument as an unsigned integer in base 8. ... Returns a String object representing this Integer's value.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 11 & JDK 11 )
January 20, 2026 - The string value of this property is then interpreted as an integer value using the grammar supported by decode and an Integer object representing this value is returned. 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.
🌐
Java Almanac
javaalmanac.io › jdk › 1.2 › api › java › lang › Integer.html
Java Platform 1.2 API Specification: Class Integer
The Class object representing the primitive type int. ... Constructs a newly allocated Integer object that represents the primitive int argument.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 17 & JDK 17)
April 21, 2026 - Returns a string representation of the integer argument as an unsigned integer in base 8. ... Returns a String object representing this Integer's value.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › int-vs-Integer-java-difference-comparison-primitive-object-types
Integer vs. int: What's the difference?
In contrast to the primitive type int, Integer is a full-blown Java class. This creates a long list of difference, such as: The Integer class is an Object while an int is a primitive type.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 6)
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.
Top answer
1 of 2
19

You skipped the intended solution:

Integer p = Integer.valueOf(1);

This pattern is known as Factory method pattern. One may ask what the benefit of this method is. Luckily, the implementation of class Integer is open-source, so let's take a look:

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

There seems to be some kind of Integer-value cache. If one requests an Integer with a value within the cache-range, Java does not create a new object, but returns a previously created one. This works because Integers are immutable. One can even control the upper cache limit with the system property java.lang.Integer.IntegerCache.high=....

And why do the other two methods of creating an Integer generate a warning? Because they were set deprecated with Java 9.

Integer#Integer(int value):

Deprecated. It is rarely appropriate to use this constructor. The static factory valueOf(int) is generally a better choice, as it is likely to yield significantly better space and time performance. [...]

Integer#Integer(String s):

Deprecated. It is rarely appropriate to use this constructor. Use parseInt(String) to convert a string to a int primitive, or use valueOf(String) to convert a string to an Integer object. [...]

And just for completeness, here is the part for Integer.valueOf(int i):

Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.


EDIT 1: Thanks to @VGR mentioning that

Integer p = 1;

is equivilant to

Integer p = Integer.valueOf(1);

This, however, is only true for int-values between -128 and 127. The behaviour is defined in JLS §5.1.7:

[...] If the value p being boxed is the result of evaluating a constant expression (§15.28) of type boolean, char, short, int, or long, and the result is true, false, a character in the range '\u0000' to '\u007f' inclusive, or an integer in the range -128 to 127 inclusive, then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.


EDIT 2: Thanks to @DorianGray, who brought the following to my attention.

While not in the JLS, the version of javac I am using (9.0.4) does compile the boxing down to Integer.valueOf(...); as it is shown in this answer by Adam Rosenfield.

2 of 2
3

Method 4, Integer p = Integer.valueOf(1); is the recommended way. The JavaDoc says:

Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

🌐
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 23 & JDK 23 [build 1])
Returns a string representation of the integer argument as an unsigned integer in base 8. ... Returns a String object representing this Integer's value.