This content originally appeared on Level Up Coding - Medium and was authored by Mohammed Atif
Primitive Type vs Wrapper Class : Which one to use in Java?
Though looks like a small thing, it makes a huge difference to your code
In this short article, I will walk you through the differences and implementations of Primitive Type and Wrapper Classes and the related consequences when used incorrectly.
What are Primitive Types and Wrapper Classes?
While many of us know what they mean, let me still give a quick overview
Primitive Types
A primitive data type specifies the size and type of variable values, and it has no additional methods.
Wrapper Classes
Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.
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 default value for wrapper classes of all types is null since they are objects.
Which one to use?
Both primitive types and wrapper classes need to be used in a code depending on how you want your code to behave. Though it sounds like a difficult thing, I will cover up most of the use cases with examples to help you get a better understanding of it.
When not sure what to use, always use the primitive type
When writing a code, drive yourself with questions instead of solutions. What does that mean?
- What happens if I use int instead of Integer?
- What happens if I use Stringconstant instead of enum
- What happens if I use Boolean instead of boolean
- And so on…
That being said, lets see how these questions can help us in using the right datatype
Let us analyse above codes
- What is the default value of latitude and longitude until the consignment is assigned? 0.0, 0.0 ? No, as it is a proper location, so it must be null until it is assigned, hence we used Float instead of float
- Can isActive be null? What will that mean? Something is either active or inactive, there is not third state for being active and hence boolean
- Can weight of a product be 0? highly unlikely. So the float that we have used in above code is incorrect as it can give incorrect result when used in the code
- In OrderManagementService.java interface I have used the same concept, for example userId needs to be a valid value and cannot be null
Other scenarios where we can drive the correct datatype from the Application itself
Database Models
- Databases can have all kind of values including null, so Model Objects or Entities must always have Wrapper Classes
DTOs (Request and Response)
- DTOs used for Request and Response may have Wrapper Classes if your API design includes nullable values
Implementations and Solutions
- Variables with local scope are always primitive type
- Field variables should be primitive, until and unless it does not satisfy the questions we ask regarding its usage
Consequences
- NullPointerExceptionwhile incorrectly using Wrapper Classes
- Unexpected defaults while incorrectly using primitive types
- Days of debugging (if you don’t know how to use debugger in IDE)
- Additional memory usage while incorrectly using Wrapper Classes
Epilogue
These concepts are good to know and easy to implement once you understand the concept behind them. Do share your views in the comment below.
Primitive Type vs Wrapper Class : Which one to use in Java? was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Mohammed Atif
Mohammed Atif | Sciencx (2021-10-08T07:24:13+00:00) Primitive Type vs Wrapper Class : Which one to use in Java?. Retrieved from https://www.scien.cx/2021/10/08/primitive-type-vs-wrapper-class-which-one-to-use-in-java/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.