There are two problems:

  1. Java uses pass by value, not by reference. Changing the reference inside a method won't be reflected into the passed-in reference in the calling method.
  2. Integer is immutable. There's no such method like Integer#set(i). You could otherwise just make use of it.

To get it to work, you need to reassign the return value of the inc() method.

integer = inc(integer);

To learn a bit more about passing by value, here's another example:

public static void main(String... args) {
    String[] strings = new String[] { "foo", "bar" };
    changeReference(strings);
    System.out.println(Arrays.toString(strings)); // still [foo, bar]
    changeValue(strings);
    System.out.println(Arrays.toString(strings)); // [foo, foo]
}
public static void changeReference(String[] strings) {
    strings = new String[] { "foo", "foo" };
}
public static void changeValue(String[] strings) {
    strings[1] = "foo";
}
Answer from BalusC on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... The Integer class wraps a value of the primitive type int in an object.
🌐
Medium
yogeshbali.medium.com › important-considerations-for-integer-value-comparison-in-java-d90b25bf29bb
Java Integer Object Caching : Why 1 == 1 is true, but 128 ...
April 27, 2025 - Therefore, both the variables will refer to the same cached Integer object. For values within the range of -128 to 127, Java uses a cache for Integer objects to optimize memory usage.
Discussions

java - How can I pass an Integer class correctly by reference? - Stack Overflow
Integer i = 0; i = i + 1; // ← I think that this is somehow creating a new object! Here is my reasoning: I know that java is pass by value (or pass by value of reference), so I think that in the following example the integer object should be incremented each time. More on stackoverflow.com
🌐 stackoverflow.com
Why doesn't Integer have a setValue(int i), and is there a way of changing its value.
Do I practice SAFE Java? Seriously, safer in what respect? Thanks -nat · Click to expand... [top posting fixed] It's harder to make coding mistakes when wrappers like Integer are immutable. And with such a simple object, it's just as easy to make a new one as it would be to call ... More on thecodingforums.com
🌐 thecodingforums.com
78
December 23, 2003
When to use int vs Integer ?
Always use the primitive unless you need Integer specific behaviour (nullable for example) More on reddit.com
🌐 r/learnjava
26
31
August 8, 2021
java - Increment a Integer's int value? - Stack Overflow
How do I increment a Integer's value in Java? I know I can get the value with intValue, and I can set it with new Integer(int i). ... Integer objects are immutable, so you cannot modify the value once they have been created. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java integer class
Java Integer Class Overview
September 1, 2008 - The Java Integer class wraps a value of primitive type int in an object.
🌐
MIT
web.mit.edu › java_v1.0.2 › www › javadoc › java.lang.Integer.html
Class java.lang.Integer
extends Number The Integer class is a wrapper for integer values. In Java, integers are not objects and most of the Java utility classes require the use of objects.
🌐
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.
🌐
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.
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.
🌐
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 ...
🌐
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, ...
🌐
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....
🌐
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.
🌐
Quora
quora.com › What-is-the-difference-between-an-Integer-and-int-in-Java
What is the difference between an Integer and int in Java? - Quora
Answer (1 of 30): To understand the difference between ‘int’ and ‘Integer’, you need to understand the difference between Primitive Types and Classes. Caution: I have tried to explain the following in simple language and may not be exactly technically or syntactically accurate.
🌐
The Coding Forums
thecodingforums.com › archive › archive › java
Why doesn't Integer have a setValue(int i), and is there a way of changing its value. | Java | Coding Forums
December 23, 2003 - Integer, like all the "wrapper" classes (Double, Character, etc.) are immutable, once created the value may not change. They are provided specifically for when you want to store a primitive value in a structure that only accepts objects, like ArrayList, LinkedList, HashTable, etc.
🌐
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.

🌐
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 - Learn about the various features of Java through a course at Udemy.com · The Integer class is a wrapper class. Its main function is to wrap the int primitive data type into an object. An Integer object has only one variable of type int.
Top answer
1 of 11
419

int is a primitive type. Variables of type int store the actual binary value for the integer you want to represent. int.parseInt("1") doesn't make sense because int is not a class and therefore doesn't have any methods.

Integer is a class, no different from any other in the Java language. Variables of type Integer store references to Integer objects, just as with any other reference (object) type. Integer.parseInt("1") is a call to the static method parseInt from class Integer (note that this method actually returns an int and not an Integer).

To be more specific, Integer is a class with a single field of type int. This class is used where you need an int to be treated like any other object, such as in generic types or situations where you need nullability.

Note that every primitive type in Java has an equivalent wrapper class:

  • byte has Byte
  • short has Short
  • int has Integer
  • long has Long
  • boolean has Boolean
  • char has Character
  • float has Float
  • double has Double

Wrapper classes inherit from Object class, and primitive don't. So it can be used in collections with Object reference or with Generics.

Since java 5 we have autoboxing, and the conversion between primitive and wrapper class is done automatically. Beware, however, as this can introduce subtle bugs and performance problems; being explicit about conversions never hurts.

2 of 11
10

An Integer is pretty much just a wrapper for the primitive type int. It allows you to use all the functions of the Integer class to make life a bit easier for you.

If you're new to Java, something you should learn to appreciate is the Java documentation. For example, anything you want to know about the Integer Class is documented in detail.

This is straight out of the documentation for the Integer class:

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

🌐
W3Schools
w3schools.com › java › java_data_types_numbers.asp
Java Numbers
Java Examples Java Videos Java ... Interview Q&A Java Certificate ... Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals....