When to use wrapper class and primitive type

JavaWrapperPrimitive Types

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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

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.

Solution 4 - Java

I would only use the wrapper types if you have to.

In using them you don't gain much, besides the fact that they are Objects.

And, you lose overhead in memory usage and time spent boxing/unboxing.

Solution 5 - Java

Practically I had encountered a situation where use of wrapper class can be explained.

I created a service class which had a long type variable

  1. If the variable is of type long - when not initialized, it will be set to 0 - this will be confusing to the user when displayed in GUI
  2. If the variable is of type Long - when not initialized, it will be set to null - this null value won't show up in GUI.

This applies to Boolean as well where values can be more confusing when we use primitive boolean(as default value is false).

Solution 6 - Java

Collections are the typical case for the simple Java wrapper objects. However, you might consider giving the Wrapper a more specific meaning in the code (value object).

IMHO there's almost always a benefit to use value objects when it boils down to readability and maintainance of the code. Wrapping simple data structures inside of objects when they have certain responsibilities often simplifies the code. This is something that is very important in Domain-Driven Design.

There is of course the performance issue, but I tend to ignore that until I have the possibility to measure the performance with proper data and do more directed actions towards the problematic area. It might also be easier to understand the performance issue if the code is easy to understand as well.

Solution 7 - Java

performance of applications that are dominated by numerical calculations can benefit greatly from the use of primitives.

primitive types, one uses the == operator, but for wrapper the preferred choice is to call the equals() method.

"Primitive types considered harmful" because they mix "procedural semantics into an otherwise uniform object-oriented model.

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

Solution 8 - Java

If you want to use Collections, you must use Wrapper classes.

Primitive types, are used for arrays. Also, to represent data that has no behaviour,for example, a counter, or a boolean condition.

Since autoboxing, the "when to use primitive or wrapper" frontier has become quite fuzzy.

But remember, Wrappers are objects, so you get all the fancy Java features. For example, you can use reflexion to create Integer objects, but not int values. Wrapper classes also have methods such as valueOf.

Solution 9 - Java

If you want to create a value type. Something like a ProductSKU or AirportCode.

When a primitive type (string in my examples) defines equality, you'll want to override equality.

Solution 10 - Java

Primitive values in Java are not object. In order to manipulate these values as object the java.lang package provides a wrapper class for each of the primitive data type.

All Wrapper classes are final. The object of all wrapper classes that can be initiated are immutable that means the value in the wrapper object can not be changed.

Although, the void class is considered a wrapper class but it does not wrap any primitive values and is not initiable. It does not have public constructor, it just denotes a class object representing the keyword void.

Solution 11 - Java

When to Use Primitive Types

  • When doing a large amount of calculations, primitive types are always faster — they have much less overhead.
  • When you don’t want the variable to be able to be null.
  • When you don’t want the default value to be null.
  • If the method must return a value

When to Use Wrapper Class

  • When you are using Collections or Generics — it is required
  • If you want the MIN_SIZE or MAX_SIZE of a type.
  • When you want the variable to be able to be null.
  • When you want to default value to be null.
  • If sometimes the method can return a null value.

from https://medium.com/@bpnorlander/java-understanding-primitive-types-and-wrapper-objects-a6798fb2afe9

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGopiView Question on Stackoverflow
Solution 1 - JavapstantonView Answer on Stackoverflow
Solution 2 - JavaMatthew FlaschenView Answer on Stackoverflow
Solution 3 - JavaCodyView Answer on Stackoverflow
Solution 4 - JavajjnguyView Answer on Stackoverflow
Solution 5 - JavaMohamed AfzalView Answer on Stackoverflow
Solution 6 - JavaMattias HolmqvistView Answer on Stackoverflow
Solution 7 - JavaloknathView Answer on Stackoverflow
Solution 8 - JavaTomView Answer on Stackoverflow
Solution 9 - JavaChris MissalView Answer on Stackoverflow
Solution 10 - JavaAhmad SayeedView Answer on Stackoverflow
Solution 11 - JavaFeudaView Answer on Stackoverflow