Google+

243. Byte Wrapper Class







Pre-requisites -



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

Example of wrapping byte primitive type into an object using Byte wrapper class -

byte weight = 5;
Byte w1 = new Byte(weight);  //Wrapping the byte primitive type variable weight  into Byte wrapper class type object w1


Example of unwrapping the Byte wrapper class type object w1 into byte primitive type variable w -

byte w = w1.byteValue( ) //Unwrapping the Byte wrapper class type object w1 using byteValue( ) method to byte primitive data type variable w

Lets implement this on Eclipse IDE -

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



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


3. Now wrap the byte primitive data type variable weight into a Byte wrapper class type object w1 as shown below -


4. Now print the wrapped Byte class type object 'w1' as shown below -



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



6. Now unwrap the Byte class object w1 back to byte primitive data type w as shown below -


7. Print the unwrapped byte primitive data type 'w' as shown below -



8. Save & Run the Java Class 'ByteWrapperClassDemo.java' and observe that the byte primitive data type value 5 is printed using the unwrapped byte primitive data type 'w' in the output as shown below -



Hence we can wrap the byte primitive data type into an object using Byte Wrapper Class and unwrap the object back to the byte primitive data type using the byteValue( ) method.

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



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



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



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


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







Please comment below to feedback or ask questions.

Short Wrapper Class will be explained in the next post.





No comments: