Wrapper Class in Java
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.
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:
Now if we want to store a value into this object we can simply pass the value as:
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.