Google+

245. Long Wrapper Class







Pre-requisites -



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

Example of wrapping long primitive type into an object using Long wrapper class -

long distance= 5;
Long d1 = new Long(distance);  //Wrapping the long primitive type variable distance  into Long wrapper class type object d1


Example of unwrapping the Long wrapper class type object d1 into long primitive type variable d -

long d = d1.longValue( ) //Unwrapping the Long wrapper class type object d1 using longValue( ) method to long primitive data type variable d

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'LongWrapperClassDemo.java' with main( ) method in the existing Java Project 'Wrapper Classes' as shown below -


2. Declare a long primitive class type variable distance and assign it a value as shown below -


3. Now wrap the long primitive data type variable distance into a Long wrapper class type object d1 as shown below -


4. Now print the wrapped Long class type object 'd1' as shown below -


5. Save and Run the Java Class 'LongWrapperClassDemo.java' and observe that the long primitive data type value 5 is printed using the wrapped object 'd1' in the output as shown below -



6. Now unwrap the Long class object d1 back to short primitive data type as shown below -


7. Print the unwrapped long primitive data type 'd' as shown below - 


8. Save & Run the Java Class 'LongWrapperClassDemo.java' and observe that the long primitive data type value is printed using the unwrapped long primitive data type 'd' in the output as shown below -



Hence we can wrap the long primitive data type into an object using Long Wrapper Class and unwrap the object back to the long primitive data type using the longValue( ) method.

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

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


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



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


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


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


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






Please comment below to feedback or ask questions.

Float Wrapper Class will be explained in the next post.







No comments: