Friday, 27 January 2017

BRIEF INTRODUCTION ON JAVA WRAPPER CLASS AND ITS FUNCTIONALITY

When we talk about java we say that java is a pure object oriented programming language which means 100% OOPS. But when we refer books then we learn that java is a 99.9% OOPS. So why it is 99.9%, as we know when we talk about OOPS everything should be in terms of object. So as we know C is a POP i.e. Procedural Oriented Programming, whereas C++ is a Partially Object Oriented Programming. 

So now lets discuss about why java is 99.9% OOP. In java when we use primitive data type like int,float,double etc these all are known as primitive data type which is derived from C. So primitive data types is not an object. When we talk about object, object creates a memory in heap, but Primitive data type is not an object so it creates a memory inside stack. 

So in java there is called wrapper class also which works same as primitive data type, but it is an object and creates memory inside heap. So what actually Wrapper means, Wrapping something is known as wrapper like let us take an example of chocolate. There is a wrapper wrapped with chocolate to protect it from dust. And when we want to eat that chocolate we unwrap it and eat that chocolate. Similarly in java also we can wrap primitive data type to an Wrapper class to treat it as an object. And again we can unwrap it by using some functions. 

So now its coming in our mind that if the functionality of primitive data type and wrapper class is same then why to use wrapper class. That's a good question, so let's clear this question. As we have discussed that java is a Pure OOP so not all the time but sometime java used to work with only object like in the case of Vector, Array-list, Map etc. Here we cannot use primitive data type because it is not supported by them to perform any operations, they need object to perform some operations, so at that time we use Wrapper class instead of primitive data type. You will get more in depth how to use and where wrapper class is required in my upcoming discussion on Collection Framework.Now how to create a wrapper class and how to unwrap it. 

Lets take an example.
Line 1  int i=5;                                     // primitive data type
Line 2  Integer iobj = new Integer(i); // Boxing
Line 3  Integer jobj = i;                     // Auto-Boxing
Line 4  int j = jobj.intValue();          // Unboxing
Line 5  int k = jobj;                         // Auto Unboxing

Here in line 1 we assigned i=5. In line 2 we are passing the value of i, instead of passing the value of i we can directly assign value to it. So in line we are wrapping primitive data type value into a Wrapper class Integer iobj so it means we are wrapping it which is also known as Boxing. Similarly in line 3 we are not using the keyword new Integer() simply we are assigning i value to Integer jobj which means it will automatically wrap that into an Wrapper class. In line 4 again we are unboxing i.e. unwrapping the value of a Wrapper class to primitive datatype using the name of the Wrapper class object with intValue(). And in line 5 like Autoboxing we can also do Auto-unboxing. 

Now lets take one more example when to use Wrapper class and when to use primitive data-type.

 for(Integer i=0;i<=2;i++) 
{  
System.out.print(i+" "); 
}

Now guess will the above code execute and the answer is yes and it will print 1 2 3. Now lets take one more example -

for(int i=0;i<=2;i++) 
{  
System.out.print(i+" "); 
}

The above code will also work same as like above. But again one question comes to our mind- if both will print the same output then when to use what. So the answer is basically when you work with any kind of framework use Wrapper class as framework works with only object. But when you perform simple operation or loop operation then go for primitive data type as the processing speed of primitive data type is very fast as compared to Wrapper class datatype.


No comments:

Post a Comment