Google+

244. Short Wrapper Class








Pre-requisites -



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

Example of wrapping short primitive type into an object using Short wrapper class -

short height= 5;
Short h1 = new Short(height);  //Wrapping the short primitive type variable height  into Short wrapper class type object h1


Example of unwrapping the Short wrapper class type object h1 into short primitive type variable h -

short h = h1.shortValue( ) //Unwrapping the Short wrapper class type object h1 using shortValue( ) method to short primitive data type variable h

Lets implement this on Eclipse IDE -

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


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


3. Now wrap the short primitive data type variable height into a Short wrapper class type object h1 as shown below -



4. Now print the wrapped Short class type object 'h1' as shown below -



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


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


7. Print the unwrapped short primitive data type 'h' as shown below -


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



Hence we can wrap the short primitive data type into an object using Short Wrapper Class and unwrap the object back to the short primitive data type using the shortValue( ) method.

Now lets use the wrapped object 'h1' 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 Short wrapper class object say 'x1' as a parameter as shown below -


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


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



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



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






Please comment below to feedback or ask questions.

Long Wrapper Class will be explained in the next post.








No comments: