new Integer(123) will create a new Object instance for each call.

According to the javadoc, Integer.valueOf(123) has the difference it caches Objects... so you may (or may not) end up with the same Object if you call it more than once.

For instance, the following code:

   public static void main(String[] args) {

        Integer a = new Integer(1);
        Integer b = new Integer(1);

        System.out.println("a==b? " + (a==b));

        Integer c = Integer.valueOf(1);
        Integer d = Integer.valueOf(1);

        System.out.println("c==d? " + (c==d));

    }

Has the following output:

a==b? false
c==d? true

As to using the int value, you are using the primitive type (considering your method also uses the primitive type on its signature) - it will use slightly less memory and might be faster, but you won't be ale to add it to collections, for instance.

Also take a look at Java's AutoBoxing if your method's signature uses Integer- when using it, the JVM will automatically call Integer.valueOf() for you (therefore using the cache aswell).

Answer from Marcelo on Stack Overflow
Top answer
1 of 6
42

new Integer(123) will create a new Object instance for each call.

According to the javadoc, Integer.valueOf(123) has the difference it caches Objects... so you may (or may not) end up with the same Object if you call it more than once.

For instance, the following code:

   public static void main(String[] args) {

        Integer a = new Integer(1);
        Integer b = new Integer(1);

        System.out.println("a==b? " + (a==b));

        Integer c = Integer.valueOf(1);
        Integer d = Integer.valueOf(1);

        System.out.println("c==d? " + (c==d));

    }

Has the following output:

a==b? false
c==d? true

As to using the int value, you are using the primitive type (considering your method also uses the primitive type on its signature) - it will use slightly less memory and might be faster, but you won't be ale to add it to collections, for instance.

Also take a look at Java's AutoBoxing if your method's signature uses Integer- when using it, the JVM will automatically call Integer.valueOf() for you (therefore using the cache aswell).

2 of 6
9

public static Integer valueOf(int i)

Returns a 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.

Parameters:
i - an int value.
Returns:
a Integer instance representing i.
Since:
1.5

refer http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#valueOf%28int%29

This variant of valueOf was added in JDK 5 to Byte, Short, Integer, and Long (it already existed in the trivial case in Boolean since JDK 1.4). All of these are, of course, immutable objects in Java. Used to be that if you needed an Integer object from an int, you’d construct a new Integer. But in JDK 5+, you should really use valueOf because Integer now caches Integer objects between -128 and 127 and can hand you back the same exact Integer(0) object every time instead of wasting an object construction on a brand new identical Integer object.

private static class IntegerCache {
private IntegerCache(){}

static final Integer cache[] = new Integer[-(-128) + 127 + 1];

static {
    for(int i = 0; i < cache.length; i++)
    cache[i] = new Integer(i - 128);
}
}

public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
    return IntegerCache.cache[i + offset];
}
    return new Integer(i);
}

refer Why YOU should use Integer.valueOf(int)

EDIT

autoboxing and object creation:

The important point we must consider is that autoboxing doesn't reduce object creation, but it reduces code complexity. A good rule of thumb is to use primitive types where there is no need for objects, for two reasons:

Primitive types will not be slower than their corresponding wrapper types, and may be a lot faster. There can be some unexpected behavior involving == (compare references) and .equals() (compare values).

Normally, when the primitive types are boxed into the wrapper types, the JVM allocates memory and creates a new object. But for some special cases, the JVM reuses the same object.

The following is the list of primitives stored as immutable objects:

  • boolean values true and false

  • All byte values

  • short values between -128 and 127

  • int values between -128 and 127

  • char in the range \u0000 to \u007F

refer http://today.java.net/pub/a/today/2005/03/24/autoboxing.html#performance_issue

🌐
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 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. Parameters: s - the String to be converted to an Integer.
🌐
TutorialsPoint
tutorialspoint.com › article › create-an-integer-object-in-java
Create an Integer object in Java
December 18, 2024 - Public class Demo { public static void main(String []args) { int val = 99; Integer obj = new Integer(val); System.out.println(obj); } } The above program displays the following output ?
🌐
CodeGym
codegym.cc › java blog › java classes › java.lang.integer class
Java.lang.Integer Class
February 20, 2025 - If you are unlucky enough to need an even larger number Java has you covered with BigInteger. 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.
🌐
Medium
medium.com › javarevisited › an-interesting-interview-question-whats-the-difference-among-new-integer-127-integer-valueof-b9e4d936765d
An Interesting Interview Question: What's the Difference ...
January 10, 2025 - It’s a seemingly basic problem but is easy to overlook. First of all, let’s take a look at a piece of code and then conduct a specific analysis. Integer a = new Integer("127"); ...
🌐
Puredanger
puredanger.github.io › tech.puredanger.com › 2007 › 02 › 01 › valueof
Why YOU should use Integer.valueOf(int)
February 1, 2007 - Used to be that if you needed an Integer object from an int, you’d construct a new Integer.
Find elsewhere
🌐
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?
New Java 7 Features: Using String in the Switch ... – TheServerSide.com ... The key difference between the Java int and Integer types is that an int simply represents a whole number, while an Integer has additional properties and methods.
🌐
Coderanch
coderanch.com › t › 392891 › java › Integer-Integer-valueOf
new Integer vs Integer.valueOf (Beginning Java forum at Coderanch)
November 26, 2002 - I ran your code and watched the same trend (where init arg: 1x10^6) 801 741 781 721 841 741 811 752 801 761 811 741 831 741 801 772 801 761 831 741 831 741 821 812 801 761 791 751 831 751 822 771 801 771 811 751 821 751 842 761 811 771 821 761 832 741 841 751 Interestingly, an additional order of magnitude (I first ran 10,000,000 objs by fat finger) shows much more variance, but the same general trend. I wonder why Integer.valueOf would be so much faster than new Integer??
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
Java Integer: Definition & Examples | Study.com
In this lesson, we'll take a look at integers in Java and give some examples of integers and the code for them. At the end, you should have a good...
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › java-int-vs-integer
Java int vs Integer
June 10, 2024 - Methods: Integer class provides several useful methods such as intValue(), parseInt(), toString(), valueOf() etc. Autoboxing and Unboxing: The autoboxing and unboxing feature of Java provides automatic conversion between int and Integer.
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... int myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter = 'D'; // Character boolean myBool = true; // Boolean String myText = "Hello"; // String
🌐
51CTO
blog.51cto.com › u_16213354 › 7943761
Access Restricted
October 20, 2023 - The security policy of this website has blocked you from further access. Contact the site owner and provide the Request ID shown on this page · If you are the owner of this website: Refer to the EdgeOne Web Security Analytics documentation for further instructions
🌐
Oracle
docs.oracle.com › javame › config › cldc › ref-impl › cldc1.0 › jsr030 › java › lang › Integer.html
java.lang Class Integer
The resulting integer value is ... static Integer valueOf(String s, int radix) throws NumberFormatException · Returns a new Integer object initialized to the value of the specified String....
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-valueof-method-in-java
Integer valueOf() Method in Java - GeeksforGeeks
January 23, 2026 - The Integer.valueOf() method of the java.lang.Integer class is used to convert primitive values or strings into Integer objects. It is commonly preferred over constructors because it supports caching and better performance. valueOf() may return ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-integer-class-java
Java.lang.Integer class in Java - GeeksforGeeks
June 21, 2022 - ( To understand Autoboxing & Unboxing ...-unboxing-java/ ) x points to 200 which is present in the space of constants. Refer Fig. 1. ... Autoboxing is done again because x is an Integer class object which is directly initialized. Note: The directly initialized object(x) cannot be modified as it is a constant. When we try to modify the object by pointing to a new constant(300), ...
🌐
Coderanch
coderanch.com › t › 773726 › java › replace-statement-deprecated-constructor
Should I replace a statement with a deprecated constructor? (Java in General forum at Coderanch)
June 28, 2023 - Yes, at some point you should replace Vector with List. You can add an int to a List<Integer>. In general, whenever an assignment context expects a wrapper type such as Integer, you can assign a primitive type (such as int) directly to it. Java will automatically convert the int to an Integer.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.integer.-ctor
Integer Constructor (Java.Lang) | Microsoft Learn
[Android.Runtime.Register(".ctor", "(I)V", "")] public Integer(int value); [<Android.Runtime.Register(".ctor", "(I)V", "")>] new Java.Lang.Integer : int -> Java.Lang.Integer