You use wrapper classes when you either need a nullable value or when you can't use a primitive value for other reasons, like when storing the value in an Object variable or when using generics (due to type erasure). Generics are probably the most common usecase for wrapper classes, think something like List. You want to use primitives in most other cases as they're way more efficient. Answer from morhp on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ why use wrapper classes over primitive datatypes?
r/javahelp on Reddit: Why use Wrapper Classes over Primitive Datatypes?
February 4, 2023 -

I just started my APCSA course and am slightly confused on why a person might user Wrapper Classes instead of the more simple primitive data types.

Could someone give an example of a situation where you would want to use primitives and one where you should use a Wrapper class?

Top answer
1 of 5
4
You use wrapper classes when you either need a nullable value or when you can't use a primitive value for other reasons, like when storing the value in an Object variable or when using generics (due to type erasure). Generics are probably the most common usecase for wrapper classes, think something like List. You want to use primitives in most other cases as they're way more efficient.
2 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ wrapper classes discussion
r/learnjava on Reddit: Wrapper Classes Discussion
September 20, 2022 -

The way I understand wrapper classes is they are used to convert primitive data types into objects so you can store them in Collection classes. I also believe that this happens automatically on backend by the compiler through a process called autoboxing.

To me, someone with no real world programming experience, I find myself wondering why use primitives at all?

The wrapper classes are much more interesting because you get methods to do things, where primitives are boring. Why not just use wrapper classes for everything? Is that even possible?

My gut tells me that someone is going to say primitives are faster, but is that a valid argument considering the speed of computers today?

Top answer
1 of 3
16

The real difference is that primitive types are not reference types.

In Java type system, reference types have a common root, while primitive types do not: All reference types are subtypes of Object. In comparison, primitive types do not have a common root; each primitive type is its own special unicorn.

This means that a variable declared with Object type can hold a value of any reference type, but it cannot hold a value of any primitive type.

This is a problem if you want to create data structures that are generally useful. Consider how you would implement a "dynamic array" data structure like ArrayList or Vector. You could use an array Object[] to store the elements, and that would work for all reference types. But since primitive types don't have a common root you would have to create a separate implementation for each primitive type.

To solve this problem, wrapper classes were created. Now, instead of needing 8 separate implementations of dynamic array (1 for reference types and 7 for primitive types), you could wrap each primitive value in an object, and just use the implementation for reference types.

Primitive types and wrapper classes were created at the same time, when the first design and implementation of Java 1.0 were made. The designers did not "reuse what was already available" because nothing was available: they built everything from scratch.

Could the designers have solved the problem some other way, maybe by creating a type system that had a common root for both primitive and reference types? Yes, but they didn't, and they probably had good reasons: implementation complexity, ease of understanding, time to market, ...

2 of 3
5

Actually, this question is pretty insightful. The answers are good and correct, but the underlying question that I think is worth asking is: should primitive types exist at all? We had a lot of discussion about this at the time and I think the reason things ended up the way they did had to do with the fact that Oak (later Java) was designed as an embedded language for the IOT (internet of things). If it had been originally designed as a server language, it might have been very different. I think primitive types are a legacy idea that is actually very harmful for exactly the reasons that Joni mentions. It's possible and desirable to have everything in the system extend Object.

How could you eliminate primitives? With a smarter JVM. HotSpot could turn special primitive objects into appropriate machine code wherever possible and there wouldn't be a need for autoboxing, wrappers or difficulties with the type system being split. The only limitation that I think would need to be placed on Integer, Float, Double, etc., is that they would have to be final. This would allow these classes to be optimized anywhere possible as if they were primitives without actually having primitives in the language. You could even have syntactic abbreviations like 'int', 'float' and 'double' for these special classes, but the important thing is that the type system would be better if it was fully unified.

But you don't start trying to build such a thing when you're thinking about smart toasters. What happened to Java in terms of its wild success was a pure accident. It happened to be a solution to a lot of web-related issues just sitting there at the right time. But it wasn't planned that way, so there are a lot of things people would like to have back. I think a lot of people at Sun/Oracle would eliminate primitives in hindsight.

๐ŸŒ
Medium
medium.com โ€บ @bpnorlander โ€บ java-understanding-primitive-types-and-wrapper-objects-a6798fb2afe9
Java: Understanding Primitive Types and Wrapper Objects | by Brian Norlander | Medium
March 19, 2019 - +-----------+---------------+ | ... | false | | char | '\u0000' | +-----------+---------------+ A wrapper class is an object that encapsulates a primitive type....
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ why do collections need wrappers for primitive types?
r/java on Reddit: Why do collections need wrappers for primitive types?
March 23, 2022 -

Iโ€™m coming from C (and know a bit of C++), and this kindof confuses me. What is happening on a memory level when an int is converted to an Integer? Do all objects have some memory overhead or something? It seems like if an ArrayList uses arrays internally, then it should be able to use an array of ints.
My best guess is so that everything stored in the collection is guaranteed to have some set of functions available to it needed by the collection implementation, but in that case, why does the programmer have to worry about wrapping the types? That seems like something that should be handled under the hood.

๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-primitive-data-type-and-type-wrapper-class-What-are-some-examples
What is the difference between primitive data type and type wrapper class? What are some examples? - Quora
Answer (1 of 2): promitive datatypes one of data type in java.For this we don't create objects.All primitive data types are reserved keywords in java. Ex: java is strict type language means before you declare a varaiable you should mention what type of data it is. Below are Primitive data types...
Find elsewhere
Top answer
1 of 5
56

In Item 5, of Effective Java, Joshua Bloch says

The lesson is clear: prefer primitives to boxed primitives, and watch out for unintentional autoboxing.

One good use for classes is when using them as generic types (including Collection classes, such as lists and maps) or when you want to transform them to other type without implicit casting (for example Integer class has methods doubleValue() or byteValue().

Edit: Joshua Bloch's reason is:

// Hideously slow program! Can you spot the object creation?
public static void main(String[] args) {
    Long sum = 0L;
    for (long i = 0; i < Integer.MAX_VALUE; i++) {
         sum += i;
    }
    System.out.println(sum);
}

This program gets the right answer, but it is much slower than it should be, due to a one-character typographical error. The variable sum is declared as a Long instead of a long, which means that the program constructs about 2^31 unnecessary Long instances (roughly one for each time the long i is added to the Long sum). Changing the declaration of sum from Long to long reduces the runtime from 43 seconds to 6.8 seconds on my machine.

2 of 5
32

The standard practice is to go with the primitives, unless you're dealing with generics (make sure you are aware of autoboxing & unboxing!).

There are a number of good reasons to follow the convention:

1. You avoid simple mistakes:

There are some subtle, non-intuitive cases which often catch out beginners. Even experienced coders slip up and make these mistakes sometimes (hopefully this will be followed by swearing when they debug the code and find the error!).

The most commmon mistake is using a == b instead of a.equals(b). People are used to doing a == b with primitives so it's easily done when you're using the Object wrappers.

Integer a = new Integer(2);
Integer b = new Integer(2);
if (a == b) { // Should be a.equals(b)
    // This never gets executed.
}
Integer c = Integer.valueOf(2);
Integer d = Integer.valueOf(2);
if (c == d) { // Should be a.equals(b), but happens to work with these particular values!
    // This will get executed
}
Integer e = 1000;
Integer f = 1000;
if (e == f) { // Should be a.equals(b)
    // Whether this gets executed depends on which compiler you use!
}

2. Readability:

Consider the following two examples. Most people would say the second is more readable.

Integer a = 2;
Integer b = 2;
if (!a.equals(b)) {
    // ...
}
int c = 2;
int d = 2;
if (c != d) {
    // ...
}

3. Performance:

The fact is it is slower to use the Object wrappers for primitives than just using the primitives. You're adding the cost of object instantiation, method calls, etc. to things that you use all over the place.

Knuth's "...say about 97% of the time: premature optimization is the root of all evil" quote doesn't really apply here. He was talking about optimisations which make the code (or system) more complicated - if you agree with point #2, this is an optimization which makes the code less complicated!

4. It's the convention:

If you make different stylistic choices to 99% of the other Java programmers out there, there are 2 downsides:

  • You will find other people's code harder to read. 99% of the examples/tutorials/etc out there will use primitives. Whenever you read one you'll have the extra cognitive overhead of thinking about how it would look in the style that you're used to.
  • Other people will find your code harder to read. Whenever you ask questions on Stack Overflow you'll have to sift through answers/comments asking "why aren't you using primitives?". If you don't believe me, just look at the battles people have over things like bracket placement, which doesn't even affect the generated code!

Normally I'd list some counter-points, but I honestly can't think of any good reasons to not go with the convention here!

๐ŸŒ
Hazriq's Dev Chaos
hazriqpedia.github.io โ€บ posts โ€บ java: primitive data types vs wrapper classes
Java: Primitive Data Types vs Wrapper Classes | Hazriq's Dev Chaos
September 27, 2025 - Primitives are simple and fast, wrappers are flexible and object-friendly, and String is a special class that feels primitive but isnโ€™t. Understanding when to use each is one of those small but important skills that makes you write cleaner and more efficient Java code.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ primitive-type-vs-wrapper-class-which-one-use-java-omar-ismail
Primitive Type vs Wrapper Class: Which one to use in Java?
October 18, 2021 - A primitive data type specifies the size and type of variable values, and it has no additional methods. Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.
๐ŸŒ
Difference Between
differencebetween.com โ€บ difference-between-wrapper-class-and-vs-primitive-type-in-java
Difference Between Wrapper Class and Primitive Type in Java | Compare the Difference Between Similar Terms
March 19, 2018 - The key difference between wrapper ... is that wrapper class is used to convert a primitive type to an object and object back to primitive type while a primitive type is a predefined data type provided by the Java programming language...
๐ŸŒ
DEV Community
dev.to โ€บ abhishek_kumar_d9009a7ae6 โ€บ primitive-types-vs-wrapper-classes-2cf0
Primitive Types vs. Wrapper Classes - DEV Community
October 2, 2024 - Java provides a wrapper class for each primitive type. ... Autoboxing and Unboxing refer to the automatic conversion between primitive types and their corresponding wrapper classes.
๐ŸŒ
LabEx
labex.io โ€บ questions โ€บ what-are-the-differences-between-primitive-and-wrapper-data-types-in-java-178548
What are the differences between primitive and wrapper data types in Java? | LabEx
Wrapper classes provide additional functionality and methods that are not available in the primitive data types. For example, they allow you to perform operations such as parsing, formatting, and converting values.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java-i-o-operation-wrapper-class-vs-primitive-class-variables
Java I/O Operation โ€“ Wrapper Class vs Primitive Class Variables | GeeksforGeeks
November 15, 2022 - However, the wrapper class allows ... any null values. Wrapper classes help the Java program be completely object-oriented whereas primitive data types help in the simple declaration of values with variables....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ java-i-o-operation-wrapper-class-vs-primitive-class-variables
Java I/O Operation - Wrapper Class Vs Primitive Class Variables
July 24, 2023 - Primitive data types, such as 'int', 'float', 'boolean', and 'char' represent the basic building blocks of data in Java. They are not objects and do not have additional functionality or methods like wrapper classes.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ wrapper-class-vs-primitive-data-types-in-java-with-io
Wrapper Class vs Primitive Data Types in Java with I/O - GeeksforGeeks
November 21, 2025 - Wrapper classes convert primitive types into objects, The default value of the wrapper class is null as they are objects. Wrapper Classes enabling features such as ยท Usage in Java Collections (e.g., ArrayList<Integer>)
๐ŸŒ
Medium
konstantinmb.medium.com โ€บ understanding-primitive-types-and-wrapper-classes-in-java-a-comprehensive-guide-6013c6b1c87
Understanding Primitive Types and Wrapper Classes in Java: A Comprehensive Guide | by Konstantin Borimechkov | Medium
June 20, 2023 - Additional Functionality: Wrapper classes provide additional functionalities and utility methods that are not available in primitive types. For example, parsing, conversion, comparison, and utility methods for working with data.
๐ŸŒ
Gopi Gorantala
ggorantala.dev โ€บ why-wrapper-classes-are-used-instead-primitives-in-prod-applications
Why Are Wrapper Classes Preferred Over Primitives?
October 3, 2023 - Wrapper classes enable method overloading. If a method accepts an Object parameter, it can accept an instance of a wrapper class or a primitive, as Java performs automatic boxing/unboxing.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ any reason to use a class instead of a primitive when declaring fields?
r/learnjava on Reddit: Any reason to use a class instead of a primitive when declaring fields?
September 8, 2023 -

I was following a tutorial, and at some point a Student class was made like so:

public class Student {
    private Long id;
private String name;
private LocalDate dob;
private Integer age;

I was typing along, and just out of habit I used long and int.
Why would you use Classes instead of primitives?

Further in the tutorial, instances were created by just passing something like this:
new Student( 1L, "John", LocalDate.of(....), 40);