In Java, int is a primitive data type used to store whole numbers, while Integer is a wrapper class that encapsulates an int value as an object. This distinction is crucial:

  • int is a primitive type that stores values directly in memory (typically 32 bits). It is faster and uses less memory but cannot be used in collections or with generics.

  • Integer is a class that wraps an int value. It enables the use of integers in collections (like List<Integer>), supports null values, and provides utility methods (e.g., parseInt(), toString(), compareTo()).

Since Java 5, autoboxing and unboxing automatically convert between int and Integer:

int a = 10;
Integer b = a; // Autoboxing
int c = b;     // Unboxing

Key Constants in Integer Class:

  • Integer.MAX_VALUE: 2,147,483,647 (maximum value for int)

  • Integer.MIN_VALUE: -2,147,483,648 (minimum value for int)

  • Integer.SIZE: 32 bits

  • Integer.BYTES: 4 bytes

When to Use Which?

  • Use int for performance-critical code, loops, arithmetic, and when null is not needed.

  • Use Integer when you need nullability, generics, or object behavior (e.g., in collections, databases, or APIs expecting objects).

⚠️ Memory Note: Integer objects use more memory (~16 bytes) than int (4 bytes). Using Integer in large collections can significantly increase memory usage.

Common Operations:

  • Convert String to int: Integer.parseInt("123")

  • Convert int to String: Integer.toString(123) or String.valueOf(123)

  • Convert int to Integer: Integer.valueOf(123) (preferred over new Integer(123))

Use Integer only when necessary—otherwise, stick with int for better performance.

the Integer class is provided so that values can be boxed/unboxed in a pure OO manner. use int where appropriate unless you specifically need to use it in an OO way; in which case Integer is appropriate.

Java Int vs Integer

However, very different things are going on under the covers here. An int is a number; an > Integer is a pointer that can reference an object that contains a number.

...

An int is not an object and cannot passed to any method that requires objects. A common case is in using the provided collection classes ( List , Map , Set ) - though it is possible to write versions of these classes that provide similar capabilities to the object versions. The wrapper classes ( Integer , Double , etc) are frequently required whenever introspection is used (such as in the reflection API).

A better description of when to use one vs. the other:

Choosing between int and Integer

I'll start with how these types should be used before going into detail on why.

  • Prefer int for performance reasons
  • Methods that take objects (including generic types like List<T>) will implicitly require the use of Integer
  • Use of Integer is relatively cheap for low values (-128 to
  1. because of interning - use Integer.valueOf(int) and not new Integer(int)
  • Do not use == or != with Integer types
  • Consider using Integer when you need to represent the absence of a value (null)
  • Beware unboxing Integer values to int with null values
Answer from Mike McMahon on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 79894646 › how-can-i-check-if-a-returned-value-is-an-integer-in-java-and-c
How can I check if a returned value is an integer in Java and C? - Stack Overflow
4 days ago - When a method or function returns a value, how can I know that it has returned a value of the same data type that I was looking for? It could be an integer or a string, and even if I don't perform ...
Top answer
1 of 10
104

the Integer class is provided so that values can be boxed/unboxed in a pure OO manner. use int where appropriate unless you specifically need to use it in an OO way; in which case Integer is appropriate.

Java Int vs Integer

However, very different things are going on under the covers here. An int is a number; an > Integer is a pointer that can reference an object that contains a number.

...

An int is not an object and cannot passed to any method that requires objects. A common case is in using the provided collection classes ( List , Map , Set ) - though it is possible to write versions of these classes that provide similar capabilities to the object versions. The wrapper classes ( Integer , Double , etc) are frequently required whenever introspection is used (such as in the reflection API).

A better description of when to use one vs. the other:

Choosing between int and Integer

I'll start with how these types should be used before going into detail on why.

  • Prefer int for performance reasons
  • Methods that take objects (including generic types like List<T>) will implicitly require the use of Integer
  • Use of Integer is relatively cheap for low values (-128 to
  1. because of interning - use Integer.valueOf(int) and not new Integer(int)
  • Do not use == or != with Integer types
  • Consider using Integer when you need to represent the absence of a value (null)
  • Beware unboxing Integer values to int with null values
2 of 10
43

If you can use int do so. If the value can be null or is used as an Object e.g. Generics, use Integer

Usually it doesn't matter which one you use but often int performs slightly better.

Discussions

Force / hint of Int to java.lang.Integer? - Support - Kotlin Discussions
Is there an annotation or other way to hint to Kotlin compiler to force my field to use java.lang.Integer, WITHOUT making it nullable? I had posted months back about woes with using mybatis and it complaining about primitive ints and us having to do some whacky constructor hacks. data class ... More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
0
March 26, 2017
java - Which one to use, int or Integer - Stack Overflow
I need to create a data transfer object, which I will use for storing the records retrieved from database. In this data transfer object, I need to declare a numeric field. For that which one is bet... More on stackoverflow.com
🌐 stackoverflow.com
utilities - Checking if a number is an Integer in Java - Stack Overflow
Is there any method or quick way to check whether a number is an Integer (belongs to Z field) in Java? I thought of maybe subtracting it from the rounded number, but I didn't find any method that ... More on stackoverflow.com
🌐 stackoverflow.com
spring - int or Integer in java - Stack Overflow
I have seen many times in code that people use int or Integer to declare variable in beans. I know int is datatype and Integer is wrapper class. My question is, in which condition int or Integer sh... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Oreate AI
oreateai.com › blog › bridging-the-gap-effortlessly-converting-integers-to-longs-in-java › d1ec6d292923ec768e3c763e86b690e2
Bridging the Gap: Effortlessly Converting Integers to Longs in Java - Oreate AI Blog
2 weeks ago - This code snippet demonstrates how you can take an Integer and seamlessly transform it into a Long. The underlying mechanism in Java, often referred to as 'autoboxing' and 'unboxing', handles much of the heavy lifting for you, making these conversions feel quite natural.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-integer-class-java
Java.lang.Integer class in Java - GeeksforGeeks
June 21, 2022 - Java Collection · Last Updated : 21 Jun, 2022 · Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with an int value like converting it to a string representation, and vice-versa.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
Force / hint of Int to java.lang.Integer? - Support - Kotlin Discussions
March 26, 2017 - Is there an annotation or other way to hint to Kotlin compiler to force my field to use java.lang.Integer, WITHOUT making it nullable? I had posted months back about woes with using mybatis and it complaining about primitive ints and us having to do some whacky constructor hacks. data class InventoryPosition( var locationNumber: Int = 0, var itemNumber: Long = 0, var state: String = "", var quantity: Double = 0.0 ) { fun mybatis() = (locationNumber.toString() == itemNumber.toString()...
🌐
CodeGym
codegym.cc › java blog › java classes › java.lang.integer class
Java.lang.Integer Class
February 20, 2025 - A reference data type that "wraps" or turns its primitive little brother in a Java object. Integer is a wrapper class for its primitive bro named int. Integer in English means a whole number.
Find elsewhere
🌐
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 Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... 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
🌐
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.
🌐
Android Developers
developer.android.com › api reference › integer
Integer | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 17 & JDK 17)
January 20, 2026 - Decodes a String into an Integer. Accepts decimal, hexadecimal, and octal numbers given by the following grammar: ... + DecimalNumeral, HexDigits, and OctalDigits are as defined in section 3.10.1 of The Java Language Specification, except that underscores are not accepted between digits.
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › lang › Integer.java
jdk/src/java.base/share/classes/java/lang/Integer.java at master · openjdk/jdk
import static java.lang.String.COMPACT_STRINGS; · /** * The {@code Integer} class is the {@linkplain · * java.lang##wrapperClass wrapper class} for values of the primitive · * type {@code int}. An object of type {@code Integer} contains a · * single field whose type is {@code int}. * * <p>In addition, this class provides several methods for converting ·
Author   openjdk
🌐
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....
Top answer
1 of 4
2

My question is, in which condition int or Integer should be used and is there any advantage of either of them over another?

Well, you should use the reference type Integer whenever you have to. Integer is nothing more than a boxed int. An Object with a single field containing the specified int value.

Consider this example

public class Ex {
    int field1;
    Integer field2;

    public Ex(){}
}

In this case field1 will be initialized with the value 0 while field2 will be initialized with null. Depending on what the fields represent, both approaches might be advantageous. If field1 represents some kind of UUID, would you want it to be initialized with a zero value?

I wouldn't worry too much about the performance implications of Autoboxing. You can still optimize after you get your code running.

For more information take a look at the documentation

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html

2 of 4
0

You always use int, pretty much.

Integer should rarely be used; it is an intermediate type that the compiler takes care of for you. The one place where Integer is likely to appear is in generics, as int is simply not legal there. Here is an example:

List<Integer> indices = new ArrayList<Integer>();
int v = 10;
indices.add(v);

The above works: It compiles with no errors and does what you think it would (it adds '10' to a list of integer values).

Note that v is of type int, not Integer. That's the correct usage; you could write Integer here and the code works as well, but it wouldn't be particularly idiomatic java. Integer has no advantages over int, only disadvantages; the only time you'd use it, is if int is straight up illegal. Which is why I wrote List<Integer> and not List<int> as the latter is not legal java code (yet - give it a few versions and it may well be legal then, see Project Valhalla).

Note also that the compiler is silently converting your v here; if you look at the compiled code it is as if javac compiled indices.add(Integer.valueOf(v)) here. But that's fine, let the compiler do its thing. As a rule what the compiler emits and what hotspot optimizes are aligned; trust that what javac emits will be relatively efficient given the situation.

🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › VariableBasics › minAndMax.html
3.7. Integer Min and Max — AP CSA Java Review - Obsolete
The int type in Java can be used to represent any whole number from -2147483648 to 2147483647. Why those numbers? Integers in Java are represented in 2’s complement binary and each integer gets 32 bits of space. In 32 bits of space with one bit used to represent the sign you can represent ...
🌐
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.
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.