Benefits of using wrapper classes over primitives in Java
Asked Answered
A

6

6

At a very abstract level I do know that wrapper classes, create an object of the primitive data type but I was curious as to why do we need to use wrapper classes and what benefits do they offer over primitive data types.

Abradant answered 22/12, 2012 at 1:40 Comment(1)
You can take a look at #1570916 and #2509525Pompidou
I
9

Collections in the first place, for example,List<Integer>, you cannot use primitive int here. Actually any generic class / interface that can work with different object types like

public interface Callable<V> {
    V call() throws Exception;
}

Note that wrapping is best done using not new Integer(i) but Integer.valueOf(i) the latter will try to use cache. Unwrapping is done as Integer.intValue(). These wrapping / unwrapping of primitives are so typical operations that Java 5 introduced autoboxing / unboxing

List<Integer> list = new ArrayList<>();
list.add(1);
int i = list.get(0);

this code is automatically converted by Java compiler into

list.add(Integer.valueIf(1));
int i = list.get(0).intValue();    // this is where NullPointerException sometimes happens
Increase answered 22/12, 2012 at 1:45 Comment(0)
B
2

Wrapper classes are designed to add more functionality to the primitive types, so that they are compatible with generic code, using the Collection Framework, and many other benefits. However, they are not mean't to replace primitive types.

So, you should use wrappers only when necessary, such as when dealing with generics, because creating an object adds substantial overheads to your program. So, in normal cases, you should stick to primitives.

Bumkin answered 22/12, 2012 at 5:6 Comment(0)
M
1

A wrapper type enables a primitive to hold more contextual meaning. For instance an integer could be anything, whereas a class called Hours, for example, gives the number meaning wherever it is used.

They also enable methods to be written that mutate the said primitive in a consistent and obvious way to consumers. Have a look at Domain Driven Design for more information surrounding this point.

Misrepresent answered 22/12, 2012 at 1:42 Comment(1)
So would it suffice to say that wrapper does not offer any better performance over primitive data type, it only adds contextual meaning to a data type ?Abradant
D
0

Wrapper objects are normal objects and there references can be null. This allows the usage of a "not set" state which is impossible with primitives.

Integer no = null;    // not set

You have to use a magic value like -1 to indicate that a primitive variable is "unset".

Depilate answered 22/12, 2012 at 1:40 Comment(0)
T
0

The primitive types just hold value, the wrapper class gives it a name.

With a class name, the compiler can do some static check for you. It makes the interface more meaningful. And you can also defined some method in wrapper classes to validate the primitive values.

Normally, for a small project, i think use primitive types is just fine.

Trictrac answered 22/12, 2012 at 1:50 Comment(0)
G
0

Just imagine : your playing a game in that you need to save the status and you want to play in different computer(same game) then u decided to take status (level) to the other computer then you can continue from that state(not from the beginning).. Okay come to the questions if your current weapons status is 3. like { int weapons=1; if(you add weapons by playing some levels okay now its 3) } but you cannot able to store it as object because primitives are different.(lives in stack). so you need weapons as objects (( solution : create Integer Object)) Integer weapon = new Integer(1){ ... }

G answered 11/6, 2014 at 5:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.