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.
Importance of wrapper class:
To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
Some features of wrapper class:
Wrapper classes convert numeric strings into numeric values.
The way to store primitive data in an object.
The valueOf() method is available in all wrapper classes except Character
All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.
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:
Integer intObj=new Integer();
Now if we want to store a value into this object we can simply pass the value as:
Integer intObj=new Integer(5);
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.
Thanks for reading this blog.
Comments