Google+

Like Us On Facebook


Total Pageviews

242. Integer Wrapper Class








Pre-requisites -


Integer wrapper class is used to wrap the int primitive type into an object, whenever we need primitive type to be used as an object.

Example of wrapping int primitive type into an object using Integer wrapper class -

int marks = 85;
Integer m1 = new Integer(marks);  //Wrapping the int primitive type variable marks  into Integer wrapper class type object m1


Example of unwrapping the Integer wrapper class type object m1 into int primitive type variable m -

int m = m1.intValue( );  //Unwrapping the Integer wrapper class type object m1 using intValue( ) method to int primitive data type variable m

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Project 'WrapperClasses' as shown below -



2. Create a new Java Class 'IntegerWrapperClassDemo.java' with main( ) method as shown below -



3. Declare an integer variable marks and assign it a value as shown below -


4. Now wrap the primitive data type marks into an Integer wrapper class type object m1 as shown below -


5. Print the wrapped Integer class type object 'm1' as shown below -


6. Save & Run the Java Class 'IntegerWrapperClassDemo.java' and observe the int primitive data type value 98 is printed using the wrapped object 'm1' in the output as shown below -


7. Now unwrap the Integer class object m1 back to the int primitive data type m as shown below -


8. Print the unwrapped int primitive data type 'm' as shown below -


9. Save & Run the Java Class 'IntegerWrapperClassDemo.java' and observe the int primitive data type value 98 is printed using the unwrapped int primitive data type 'm' in the output as shown below -



Hence we can wrap the int primitive data type into an object using Integer Wrapper Class and unwrap the object back to the int primitive data type using the intValue( ) method.

Now lets use the wrapped object 'm1' by passing it by reference to other method and printing it by following the below steps.

10. Create another method inside the class say 'methodOne( )' as shown below -


11. Modify the newly created method by creating an Integer wrapper class object say 'x1' as a parameter as shown below -


12. Now write the code inside the newly created method to print the Integer wrapper class object received by the Integer wrapper Class object parameter 'x1' as shown below -


13. Now call the method methodOne( ) from the main( ) method by passing the wrapped reference 'm1' as shown below -



14. Save & Run the Java Class 'IntegerWrapperClassDemo.java' and observe that the int primitive type value wrapped in the form of 'm1' Integer class type object is passed to the newly created method methodOne( ) and the passed wrapped object is received by the Integer class type object parameter and got printed in the output using the code inside the methodOne( ) as shown below -



Hence we can wrap the int primitive data type into object and pass the wrapped objects by reference to the other methods.






Please comment below to feedback or ask questions.

Byte Wrapper Class will be explained in the next post.







2 comments:

Ranjith Kumar said...

Hi Arun,

I have a question about the parameter in the method methodone(Integer x1). I called this method by passing the int value, even then the method is called and the value is printed i.e methodone(marks) will also print the same result.

Please shed some light on this.

Thanks,
Ranjith

Arun Motoori said...

@Ranjith - Good question. Your question is answered in the upcoming posts.

Blog Index