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
๐ŸŒ
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
The AP CSA Exam covers Java 7 which does allow using the constructor. // 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; ...
๐ŸŒ
Java Guides
javaguides.net โ€บ 2018 โ€บ 08 โ€บ integer-wrapper-class-in-java.html
Integer Wrapper Class in Java
July 5, 2024 - 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. In addition, this class provides several methods for converting an int to a String and a String to an ...
People also ask

Are Integer and Double part of java.lang?
Yes. Integer and Double are part of the java.lang package, so they are available automatically and do not require an import statement.
๐ŸŒ
fiveable.me
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
What is autoboxing in Java?
Autoboxing is the automatic conversion from a primitive to its matching wrapper class, such as int to Integer or double to Double. It happens when a primitive is assigned to a wrapper variable or passed to a method expecting a wrapper.
๐ŸŒ
fiveable.me
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
What is a wrapper class in AP Computer Science A?
A wrapper class gives a primitive value an object form. In AP CSA, the tested wrapper classes are Integer for int values and Double for double values.
๐ŸŒ
fiveable.me
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
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ wrapper
Java Wrapper Class (With Examples)
We can also use the valueOf() method to convert primitive types into corresponding objects. class Main { public static void main(String[] args) { // create primitive types int a = 5; double b = 5.65; //converts into wrapper objects Integer aObj = Integer.valueOf(a); Double bObj = Double.valueOf(b); ...
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.

๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-create-integer-wrapper-object-420416
How to create Integer wrapper object | LabEx
In Java programming, understanding how to create Integer wrapper objects is crucial for developers seeking to work with numeric data types effectively. This tutorial explores various methods of creating Integer objects, demonstrating the flexibility and power of Java's wrapper classes in handling ...
๐ŸŒ
Codekru
codekru.com โ€บ home โ€บ integer wrapper class in java
Integer Wrapper class in Java - Codekru
November 20, 2022 - We can compare two integer objects using the equals() method or the comparison operator ( == ). equals() method compares the primitive values stored by a wrapper instance, whereas == compares the object references only. Letโ€™s show you this with an example. public class Codekru { public static void main(String[] args) { Integer i1 = Integer.valueOf(1234); Integer i2 = Integer.valueOf(1234); if (i1.equals(i2)) { System.out.println("Both are equal"); } else { System.out.println("Both aren't equal"); } } }
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_wrapper_classes.asp
Java Wrapper Classes
Sometimes you must use wrapper classes, for example when working with Collection objects, such as ArrayList, where primitive types cannot be used (the list can only store objects): ArrayList<int> myNumbers = new ArrayList<int>(); // Invalid ...
Find elsewhere
๐ŸŒ
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 - Wrapper classes let you use Integer and Double objects in places that need objects instead of primitives, like an ArrayList. Java handles conversions automatically through autoboxing from primitive to wrapper and unboxing from wrapper to primitive, and you can turn a String into a number with Integer.parseInt or Double.parseDouble.
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ wrapper classes in java (with examples)
Wrapper Classes in Java (with Examples) - Scientech Easy
February 3, 2025 - Output: Displaying values of Wrapper class objects: Character object: a Byte object: 10 Integer object: 20 Float object: 18.6 Double object: 250.5 Displaying unwrapped values: char value: a byte value: 10 int value: 20 float value: 18.6 double value: 250.5 ยท 1. Wrapper class in Java provides several constructors, constants, and conversion methods for manipulating various primitive data type values.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ csawesome2 โ€บ topic-4-7-wrapper-classes.html
4.7 Wrapper Classes - Integer and Double
The wrapper classes are also used ... examples of creating Integer and Double objects. In Java version 7, you can use a constructor like new Integer(2) to create an object with the value 2 in it....
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 09 โ€บ wrapper-class-in-java
Wrapper class in Java
November 8, 2022 - Lets take few examples to understand how the conversion works: public class JavaExample{ public static void main(String args[]){ //Converting int primitive into Integer object int num=100; Integer obj=Integer.valueOf(num); System.out.println(num+ " "+ obj); } }
๐ŸŒ
W3Resource
w3resource.com โ€บ java-tutorial โ€บ java-wrapper-classes.php
Java Wrapper Classes - w3resource
August 19, 2022 - 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. ... As explain in above table all wrapper classes (except Character) take String as argument constructor. Please note we might get NumberFormatException if we try to assign invalid argument in the constructor. For example ...
๐ŸŒ
Open Book Project
openbookproject.net โ€บ thinkcs โ€บ archive โ€บ java โ€บ english โ€บ app01.htm
Wrapper classes
In other words, you can wrap a primitive value up in an object, which is useful if you want to invoke a method that requires an object type. The most straightforward way to create a wrapper object is to use its constructor: Integer i = new Integer (17); Double d = new Double (3.14159); Character ...
๐ŸŒ
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.
๐ŸŒ
Nus-cs2030s
nus-cs2030s.github.io โ€บ 2021-s2 โ€บ 16-wrapper.html
16. Wrapper Class - CS2030S Programming Methodology II
As conversion back-and-fro between ... type and its wrapper class. ... The first statement is an example of auto-boxing, where the primitive value int of 4 is converted into an instance of Integer....
๐ŸŒ
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
For every primitive type in Java, there is a built-in object type called a wrapper class. The wrapper class for int is called Integer, and for double it is called Double. Sometimes you may need to create a wrapped object for a primitive type so that you can give it to a method that is expecting ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ wrapper-classes-java
Wrapper Classes in Java - GeeksforGeeks
class Geeks { public static void main(String[] args) { int b = 357; // Autoboxing: primitive int -> Integer object Integer a = b; System.out.println("The primitive int b is: " + b); System.out.println("The Integer object a is: " + a); } } ... ...
Published ย  April 6, 2026
๐ŸŒ
Hero Vired
herovired.com โ€บ learning-hub โ€บ blogs โ€บ wrapper-class-in-java
What is Wrapper Classes in Java: Types, Define, example
January 21, 2025 - Wrapper classes also provide a way to handle Exceptions in Java. For example, the Integer wrapper class provides the parseInt() method, which is used to parse an int from a String.
๐ŸŒ
Medium
medium.com โ€บ @quipoin04 โ€บ wrapper-classes-in-java-complete-guide-with-examples-150b77f47797
Wrapper Classes in Java โ€” Complete Guide with Examples | by Quipoin | Medium
October 7, 2025 - Primitive TypeWrapper ... passed, and manipulated like objects. int num = 10; // primitive Integer obj = Integer.valueOf(num); // wrapper class object System.out.println(obj);...