Wrapper Class in Java

Sakhawat Hossain
2 min readJan 11, 2019

--

Java is an object oriented programming language. Yet, it is not a 100% pure object oriented language. Because of primitive data types.

We all know about primitive data types in java. Like int, char,float,string etc. We always kind of habituate to work with the primitive data types. But sometimes there is a case where primitive data types is not enough. In that kind of case we can use Wrapper Class. Let’s see the wrapper class corresponding to the primitive data types.

Wrapper CLasses In JAVA

So you can see that every wrapper class begins with upper case.

What are wrapper classes?

A wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution.

So generally the variable we declare it’s not an object. According to java oop everything in java should be object. Either we declare a variable or others. When we declare int a; which is not an object. It is a primitive types which holds integer data’s.

But whenever we talk about Integer which is not a data types. It is a class. All of we know that how to declare an object. So let’s create an object:

Creating an instance of wrapper class

Now if we want to store a value into this object we can simply pass the value as:

Creating and assigning the size of a wrapper class

So that’s the way of working with wrapper class. At last we should always keep in mind that java follows object oriented concepts so we should apply this concepts in everywhere.

--

--

Sakhawat Hossain
Sakhawat Hossain

Written by Sakhawat Hossain

Senior Software Engineer | Android | iOS | Java | Kotlin | Flutter

No responses yet