Primitive Type vs Wrapper Class: Which one to use in Java?

Primitive Type vs Wrapper Class: Which one to use in Java?

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?

  • What happens if I use?int?instead of?Integer?
  • What happens if I use?Stringconstant instead of an enum
  • What happens if I use?Boolean?instead of?boolean
  • And so on…

That being said, let's see how these questions can help us in using the right datatype

No alt text provided for this image


No alt text provided for this image
No alt text provided for this image

Let us analyze 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 no third state for being active, and hence?boolean
  • Can the 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 results when used in the code
  • In?the 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 kinds 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

No alt text provided for this image

  • Variables with the 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 the 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.

Thanks TO : Mohammed Atif





要查看或添加评论,请登录

社区洞察

其他会员也浏览了