Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use wrapper class and primitive type

When I should go for wrapper class over primitive types? Or On what circumstance I should choose between wrapper / Primitive types?

like image 518
Gopi Avatar asked Oct 15 '09 05:10

Gopi


People also ask

When would you use a wrapper class?

The wrapper classes in java can be used when it is required to use the primitive types as objects. Moreover, wrapper classes also contain methods that unwrap the object and return the data type. The classes only handle objects in java. util package.

What is the difference between primitive type and wrapper type?

One major difference from the explanation above is, default values of the primitive types depend on type like int is 0, char is \u0000, boolean is false, etc but the default value for wrapper classes of all types is null since they are objects.

Is wrapper class primitive type?

We cannot say that Wrapper classes themselves are Primitive types. They just wrap the primitive types. The Byte , Short , Integer , Long , Float , and Double wrapper classes are all subclasses of the Number class.


3 Answers

Others have mentioned that certain constructs such as Collections require objects and that objects have more overhead than their primitive counterparts (memory & boxing).

Another consideration is:

It can be handy to initialize Objects to null or send null parameters into a method/constructor to indicate state or function. This can't be done with primitives.

Many programmers initialize numbers to 0 (default) or -1 to signify this, but depending on the scenario, this may be incorrect or misleading.

This will also set the scene for a NullPointerException when something is being used incorrectly, which is much more programmer-friendly than some arbitrary bug down the line.

like image 154
pstanton Avatar answered Nov 07 '22 06:11

pstanton


Generally, you should use primitive types unless you need an object for some reason (e.g. to put in a collection). Even then, consider a different approach that doesn't require a object if you want to maximize numeric performance. This is advised by the documentation, and this article demonstrates how auto-boxing can cause a large performance difference.

like image 29
Matthew Flaschen Avatar answered Nov 07 '22 08:11

Matthew Flaschen


In my opinion, if my class members are wrapper variables, it does not rely on default values, which is developer friendly behavior.

1.

class Person {
   int SSN ; // gets initialized to zero by default 
}

2.

class PersonBetter {
  Integer SSN; //gets initialized to null by default
}

In the first case, you cannot keep SSN value uninitialized. It may hurt if you are not checking if the value was set before you attempt to use it.

In the second case, you can keep SSN initialized with null. Which can lead to NullPointerException but it is better than unknowingly inserting default values(zero) as SSN into to the database whenever you attempt to use it without initializing SSN field.

like image 17
Cody Avatar answered Nov 07 '22 07:11

Cody