🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
April 21, 2026 - Constructs a newly allocated Integer object that represents the specified int value.
🌐
CodeGym
codegym.cc › java blog › java classes › java.lang.integer class
Java.lang.Integer Class
February 20, 2025 - public Integer(String s) throws NumberFormatException. Here s is a string representation of the int value. This constructor creates an Integer object that was initialised with the int value provided by string representation.
🌐
TutorialsPoint
tutorialspoint.com › create-an-integer-object-in-java
Create an Integer object in Java
December 18, 2024 - In Java, a constructor is a special method whose name is exactly the same as the class name. The following example creates an Integer object using the Integer constructor. This means we will create an instance of the Integer wrapper class by ...
🌐
MIT
web.mit.edu › java_v1.0.2 › www › javadoc › java.lang.Integer.html
Class java.lang.Integer
In Java, integers are not objects and most of the Java utility classes require the use of objects. Thus, if you needed to store an integer in a hashtable, you would have to "wrap" an Integer instance around it. ... The maximum value an Integer can have. ... The minimum value an Integer can have. ... Constructs an Integer object initialized to the specified int value.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 7 )
Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.
🌐
Google
developers.google.com › j2objc › integer
Integer | J2ObjC | Google for Developers
April 8, 2021 - Integer objects can be created from int values or strings using constructors and factory methods like valueOf.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-integer-class-java
Java.lang.Integer class in Java - GeeksforGeeks
June 21, 2022 - Integer(String s): Creates an Integer object initialized with the int value provided by string representation. Default radix is taken to be 10.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › integer class in java
Integer Class in Java| Scaler Topics
April 14, 2024 - In the above code, the Integer string is passed using a constructor so that it will create an object of the Integer class with an Integer value of 12. In the 8th line of code, the Integer class Object is created using int value, which will create an object of the Integer class with value 12.
🌐
ZetCode
zetcode.com › java › lang-integer
Java Integer Class - Complete Tutorial with Examples
By leveraging these methods and constants, developers can efficiently work with integer values while ensuring compatibility with Java's object-oriented features. Integer objects can be created using the valueOf method or autoboxing. The valueOf method is preferred, as it may reuse cached instances ...
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.

🌐
Udemy
blog.udemy.com › home › working with the integer class in the java programming language
Working with the Integer Class in the Java Programming Language - Udemy Blog
December 4, 2019 - This constructor allocates an Integer Object that represents the number value of the argument passed to the constructor. The argument must be a textual representation of an integer value. Example: Integer sx = new Integer(“6734”); // create new Integer object by passing a parsable integer 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 - Constructs a newly allocated Integer object that represents the int value indicated by the String parameter. The string is converted to an int value in exactly the manner used by the parseInt method for radix 10. ... NumberFormatException - if the String does not contain a parsable integer.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit2-Using-Objects › topic-2-8-IntegerDouble.html
2.8. Wrapper Classes - Integer and Double — CSAwesome v1
The wrapper class for int is called Integer, and for double it is called Double. Sometimes you may need to create a wrapped object for a primitive type so that you can give it to a method that is expecting an object. To wrap a value, call the constructor for the wrapper class in earlier versions of Java. In Java 9 on, this is deprecated which means it’s not the best ...
🌐
Reddit
reddit.com › r/learnjava › when to use int vs integer ?
r/learnjava on Reddit: When to use int vs Integer ?
August 8, 2021 -
import java.util.*;
class Main {  
  public static void main(String args[]) { 
	Integer[] a = {1,2};
	int[] b = {1,2};
	System.out.println(Arrays.toString(a));
	System.out.println(Arrays.toString(b));

  } 
}

Hi all, trying to learn Java from python background and run into some trouble.

So I have read that Integer is the object class and int is the binary primitive, and that Java compiler is now able to convert between them when nesessary.

But when should I use the primitive vs the object ? Generally speaking, all I can think of right now is that we should always use the primitives unless we may need to cast to another type in the future so then use object ones.

🌐
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 - Constructs a newly allocated Integer object that represents the int value indicated by the String parameter. The string is converted to an int value in exactly the manner used by the parseInt method for radix 10. ... NumberFormatException - if the String does not contain a parsable integer.
🌐
Study.com
study.com › business courses › java programming tutorial & training
Java: Int vs. Integer | Study.com
It's possible to create (i.e. construct via the 'new' operator) an Integer object from an int primitive data type, as well as from a String object (which encapsulates a series of text characters).
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-integer-class-methods
Java.lang.Integer class and its methods - GeeksforGeeks
July 23, 2025 - java.lang.Integer wraps integer data type to an object containing a single field having datatype is int. ... Integer (int arg) : Constructs integer object representing an int value.
🌐
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 - Constructs a newly allocated Integer object that represents the int value indicated by the String parameter. The string is converted to an int value in exactly the manner used by the parseInt method for radix 10. ... NumberFormatException - if the String does not contain a parsable integer.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › java_lang_integer.htm
Java - Integer class
An object of type Integer contains a single field whose type is int. Following is the declaration for java.lang.Integer class −