Firstly you can just use the primitive types:

int x = 12;
double d = 3.3456;

Secondly, you can optionally use the object wrappers for those numbers:

Integer x = new Integer(12);
Double d = new Double(3.3456);

Third, if you want the string representation you can do this simply with:

String s = Integer.toString(12);

or just:

int x;
String s = "x = " + x;

or even printf() like syntax:

int x = 12;
String s = String.format("x = %d", x);

Lastly, Java since version 5 has supported auto-boxing and unboxing, which may confuse you if you're not expecting it. For example:

Integer i = 1234; // autoboxes int to Integer

and

int i = new Integer(1234); // autounboxes Integer to an int

Just use the primitive types unless you need the Number wrappers. The most common reason to use them is to put them in collections (Lists, etc).

Answer from cletus on Stack Overflow
🌐
Java Guides
javaguides.net › 2018 › 08 › integer-wrapper-class-in-java.html
Integer Wrapper Class in Java
July 5, 2024 - The Integer class in Java is a wrapper class for the primitive data type int. It is part of the java.lang package and provides various methods to work with integers, such as parsing, converting, and comparing them.
🌐
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
// in older versions of Java (and on the AP exam) Integer i = new Integer(2); // create an object with 2 in it Double d = new Double(3.5); // create an object with 3.5 in it // in newer versions of Java (9+) Integer i = 2; Double d = 3.5; These wrapper classes (defined in the java.lang package) ...
🌐
Decodejava
decodejava.com › java-integer-class.htm
Java Integer Class- Decodejava.com
Exception in thread "main" ... i = new Integer(10); //Converting an int value argumet to wrapper Integer object System.out.println("Value in wrapped Integer object, i : "+ i); byte b = i.byteValue(); //Returns a primitive byte value out of a wrapped Integer object ...
🌐
Codekru
codekru.com › home › integer wrapper class in java
Integer Wrapper class in Java - Codekru
November 20, 2022 - Integer wrapper classes cache the wrapper objects for values between -128 to 127. So, if we again make an integer object of the values between -128 and 127, no new object will be created, and the object will be picked from the cache made by the integer class.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... The Integer class wraps a value of the primitive type int in an object.
Top answer
1 of 5
10

Firstly you can just use the primitive types:

int x = 12;
double d = 3.3456;

Secondly, you can optionally use the object wrappers for those numbers:

Integer x = new Integer(12);
Double d = new Double(3.3456);

Third, if you want the string representation you can do this simply with:

String s = Integer.toString(12);

or just:

int x;
String s = "x = " + x;

or even printf() like syntax:

int x = 12;
String s = String.format("x = %d", x);

Lastly, Java since version 5 has supported auto-boxing and unboxing, which may confuse you if you're not expecting it. For example:

Integer i = 1234; // autoboxes int to Integer

and

int i = new Integer(1234); // autounboxes Integer to an int

Just use the primitive types unless you need the Number wrappers. The most common reason to use them is to put them in collections (Lists, etc).

2 of 5
7

What you're looking at in the first example is called autoboxing / autounboxing. Java (starting with version 5) will automatically convert between 5 and Integer.valueOf(5), which constructs a wrapper of type Integer from an int primitive. But the latter (Integer x = new Integer(12)) is absolutely correct.

It doesnt work that way in the second case, however, if you wanted to write something like 5.toString(). Autoboxing only occurs when assigning a primitive to a wrapper type, or passing a primitive where the wrapper type is expected, etc. Primitive types are not Objects and thus have no properties to be referenced.

Re: "why new", it's because all reference (non-primitive) types in Java are allocated dynamically, on the heap. So (autoboxing aside), the only way to get an Integer (or other reference type) is to explicitly allocate space for one.

🌐
Classpath
developer.classpath.org › doc › java › lang › Integer-source.html
Source for java.lang.Integer (GNU Classpath 0.95 Documentation)
1: /* Integer.java -- object wrapper for int 2: Copyright (C) 1998, 1999, 2001, 2002, 2004, 2005 3: Free Software Foundation, Inc. 4: 5: This file is part of GNU Classpath.
🌐
Duke
www2.cs.duke.edu › csed › ap › subset › doc › ap › java › lang › Integer.html
Integer (Advanced Placement Computer Science Java Subset Specification)
The four methods below are those ... for the wrapper class Integer. An object whose type is Integer encapsulates a single int value. The methods in the APCS Java subset are applicable to Integer objects. Two static methods not in the subset are often useful in practice.
Find elsewhere
🌐
Nus-cs2030s
nus-cs2030s.github.io › 2021-s2 › 16-wrapper.html
16. Wrapper Class - CS2030S Programming Methodology II
The wrapper class for int is called Integer, for double is called Double, etc. A wrapper class can be used just like every other class in Java and behave just like every other class in Java. In particular, they are reference types and their instances can be created with new; instances are stored ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome2 › topic-4-7-wrapper-classes.html
4.7 Wrapper Classes - Integer and Double
// in older versions of Java Integer i = new Integer(2); // create an object with 2 in it Double d = new Double(3.5); // create an object with 3.5 in it // in newer versions of Java (9+) Integer i = 2; Double d = 3.5; These wrapper classes are also useful because they have some special values ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit2-Using-Objects › topic-2-8-IntegerDouble.html
2.8. Wrapper Classes - Integer and Double — CS Java
// in older versions of Java Integer i = new Integer(2); // create an object with 2 in it Double d = new Double(3.5); // create an object with 3.5 in it // in newer versions of Java (9+) Integer i = 2; Double d = 3.5; These wrapper classes (defined in the java.lang package) are also useful ...
🌐
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
* 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}. * * ...
Author   openjdk
🌐
W3Resource
w3resource.com › java-tutorial › java-wrapper-classes.php
Java Wrapper Classes - w3resource
August 19, 2022 - The first statement declares an int variable named x and initializes it with the value 25. The second statement instantiates an Integer object. The object is initialized with the value 33 and a reference to the object is assigned to the object variable y. Below table lists wrapper classes in Java API with constructor details.
🌐
Programiz
programiz.com › java-programming › wrapper
Java Wrapper Class (With Examples)
The wrapper classes in Java are used to convert primitive types (int, char, float, etc) into corresponding objects. Each of the 8 primitive types has corresponding wrapper classes. We can also use the valueOf() method to convert primitive types into corresponding objects. class Main { public ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... The Integer class wraps a value of the primitive type int in an object.
🌐
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.
🌐
Fiveable
fiveable.me › home › ap computer science a › unit 4 – data collections study guides › topic: 4.7
AP CSA 4.7: Integer and Double Wrapper Classes | Fiveable
June 9, 2026 - The Integer class wraps an int, and the Double class wraps a double. Both live in the java.lang package, which is available automatically, so you do not write an import statement to use them.
🌐
LabEx
labex.io › tutorials › java-how-to-create-integer-wrapper-object-420416
How to create Integer wrapper object | LabEx
In Java, the Integer wrapper class is a fundamental part of the Java language that encapsulates the primitive int data type within an object.
🌐
Scaler
scaler.com › home › topics › integer class in java
Integer Class in Java| Scaler Topics
April 14, 2024 - The Java.lang.Number package contains the Java Integer class. An int value can be successfully handled using the methods in the integer class, which is a wrapper class for the primitive type int. For example, an int value can be efficiently represented as a string and vice versa.