Primitive Type vs Wrapper Class: Which one to use in Java?
Omar Ismail
Senior Software Engineer @ Digitinary | Java 8 Certified? | Spring & Spring Boot?????? | AWS? | Microservices ?? | RESTFul Apis & Integrations ?? FinTech ?? | Open Banking ?? | Digital Payments and Transformation??
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 the 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 them.
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?
That being said, let's see how these questions can help us in using the right datatype
领英推荐
Let us analyze above codes
Other scenarios where we can drive the correct datatype from the Application itself
Database Models
DTOs (Request and Response)
Implementations and Solutions
Consequences
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.
Thanks TO : Mohammed Atif