• Integer.class is, as you say, a reference to the Class object for the Integer type.

  • int.class is, similarity, a reference to the Class object for the int type. You're right that this doesn't sound right; the primitives all have a Class object as a special case. It's useful for reflection, if you want to tell the difference between foo(Integer value) and foo(int value).

  • Integer.TYPE is just an alias for int.class


You can get a sense of this with a simple program:

public class IntClasses {
  public static void main(String[] args) {
    Class<Integer> a = int.class;
    Class<Integer> b = Integer.TYPE;
    Class<Integer> c = Integer.class;

    System.out.println(System.identityHashCode(a));
    System.out.println(System.identityHashCode(b));
    System.out.println(System.identityHashCode(c));
  }
}

Example output (it'll be different each time, but the first two will always be the same, and the third will virtually always be different):

366712642
366712642
1829164700
Answer from yshavit 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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-integer-class-java
Java.lang.Integer class in Java - GeeksforGeeks
June 21, 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.
Discussions

java - Integer.class vs int.class - Stack Overflow
That last sentence may be in the javadocs, but it's wrong. :) You can also access those objects via int.class, boolean.class etc. I don't know when that functionality came in, so maybe the docs are just out of date there? 2014-03-18T14:09:02.307Z+00:00 ... @EvgeniyDorofeev how is this supposed to be the answer? The question was what's the difference between Integer... More on stackoverflow.com
🌐 stackoverflow.com
Why doesn't Integer have a setValue(int i), and is there a way of changing its value.
According to the javadocs for ... into an integer, but this implementation technique is not required by the JavaTM programming language." Sounds good enough for me! As to the equals, only an implementation can decide what equality means. The toString method is often used to generate something like classname@hashCode, ... More on thecodingforums.com
🌐 thecodingforums.com
78
December 23, 2003
java - How can I pass an Integer class correctly by reference? - Stack Overflow
I am hoping that someone can clarify what is happening here for me. I dug around in the integer class for a bit but because integer is overriding the + operator I could not figure out what was going More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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.
🌐
CodeGym
codegym.cc › java blog › java classes › java.lang.integer class
Java.lang.Integer Class
February 20, 2025 - Integer in java is a whole number that fits in 32 bits. If you want a larger number you are welcome to use Java Long numbers. They have 64 bits at their disposal. If you are unlucky enough to need an even larger number Java has you covered with ...
🌐
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
🌐
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, ...
🌐
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
🌐
Baeldung
baeldung.com › home › java › java numbers › integer.class vs integer.type vs int.class
Integer.class vs Integer.TYPE vs int.class | Baeldung
January 8, 2024 - This helps to identify the inheritance relationship with the Number class. Integer.TYPE is a special constant in Java that represents the primitive type int. We can use Integer.TYPE to distinguish between primitive types and their corresponding wrapper classes.
🌐
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 - Click to expand... Just create a new Integer, constructed with the new value. And, generally speaking, I would use the native 'int' rather than the 'Integer' class to store integer values unless you absolutely have to use the latter e.g.
🌐
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.
🌐
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.

🌐
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 2
1

values can be stored in an object only if you have called a field in the class or method.

This statement doesn't make any sense. I can't tell if it is wrong or right, it just... doesn't make sense.

it shouldn't be able to call its value field without instantiating an object since value isn't static. I just can't wrap my head around how this is even possible.

Javac sees that and goes: Hey, you're shoving a square peg (a constant of type int) into a round hole (an object typed variable) - this doesn't make sense. Except, explicitly called out in the Java Language Specification, if an int expression shows up in a place where int expressions should not be, but an Integer expression works fine, then 'apply autoboxing'.

'apply autoboxing' means: Assume the programmer meant to write: Integer.valueOf(the expression) instead of what they wrote.

Thus:

Integer x = 56;
Integer x = Integer.valueOf(56);

are identical. They are so identical, they produce the same bytecode. That call to valueOf is in your class file, no matter how you write it. Let's test:

> cat Test.java
class Test {
    Integer x = 56;
}

> javac Test.java
 (`javap` is part of java distributions and prints bytecode)
> javap -private -c Test
Compiled from "Test.java"
class Test {
  java.lang.Integer x;

  Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: bipush        56
       7: invokestatic  #7                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      10: putfield      #13                 // Field x:Ljava/lang/Integer;
      13: return
}

Look at that - calls to the static method Integer.valueOf, even though Integer.valueOf appears exactly nowhere in our Test.java file.

So, how does Integer.valueOf work?

Essentially, it works like this:

package java.lang;

public class Integer {
    private static final Integer[] CACHE = generateCache();

    private final int value;
    public Integer(int value) {
        this.value = value;
    }

    private static Integer[] generateCache() {
        for (int i = 0; i < 256; i++) {
            CACHE[i - 128] = new Integer(i);
         }
    }

    public static Integer valueOf(int v) {
        if (v >= -128 && v < 128) return CACHE[i + 128];
        return new Integer(v);
    }
}

In other words, the Integer class pre-creates 256 integer objects, from -128 to +127 (why those values? Eh, cuz sun thought those would come up so often, it'd be worthwhile to cache them, 25 years ago). If you valueOf() with a value between -128 and +127 you get one of the caches. If you valueOf outside of it, you get a new object.

We can prove that too:

> cat Example.java
class Example {
    public static void main(String[] args) {
        Integer a = -100;
        Integer b = -100;
        Integer c = 200;
        Integer d = 200;
        System.out.println("a == b? " + (a == b));
        System.out.println("c == d? " + (c == d));
        System.out.println("ident-hash a: " + System.identityHashCode(a));
        System.out.println("ident-hash b: " + System.identityHashCode(b));
        System.out.println("ident-hash c: " + System.identityHashCode(c));
        System.out.println("ident-hash d: " + System.identityHashCode(d));
    }
}
> java Example.java
a == b? true
c == d? false
ident-hash a: 1880794513
ident-hash b: 1880794513
ident-hash c: 1991313236
ident-hash d: 736778932

This code shows that for -100, that Integer.valueOf call is returning the exact same object (== compares reference identity, i.e.: Is it the same object, when used on objects, it does not compare values. a and b are just pointing to the same object, whereas c and d are pointing at different objects that have the same values).

It's like a bunch of identical houses build in the suburbs someplace: a and b are like pieces of paper with the same address written on them, whereas c and d are like pieces of paper with a different address on each, but the two houses at these addresses look the same.

2 of 2
1

The language can and does make any exceptions it wants for object types that are built into the language. In short, you can do that in Java, because Java says you can.

Specifically, the conversion of a primitive value (int, long, etc) to one of the wrapper classes (Integer, Long, etc) is called 'autoboxing' and, like the name says, is performed automatically.

Why is this allowed? Because it's highly convenient for users. Along with automatic unboxing, it allows you to get quite close to treating wrapper classes like the primitive values they contain.


Some terminology: you only 'call' methods, not variables or fields. 'Call' means 'transfer control to'.

In classes like Integer, you don't even know what fields it has. All you know is that it can store an integral value in a certain range. Values can only be entered into an Integer by calling a constructor, or one of the static methods that will return an Integer.

Documentation for Integer, in case you haven't seen it. Note that there are no operations that 'change' the value of an Integer; we say it is 'immutable'. If you want a different value, you can only make a new 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 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 ... Non-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later chapter)
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Integer Java Class Example - Java Code Geeks
May 31, 2021 - The Integer Java 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.