Boxing in Java

Previous
Next

Boxing:

  • Converting primitive byte to object Byte.
  • All byte data conversion methods belong to Byte class only.
  • In Byte class, there is a method that can converts primitive byte into Object Byte.
  • Library is a huge collection of methods.
  • We need to analyze the prototype of method before search in library.
  • General analysis of programmer in byte conversion as follows.

Actual method belongs to Wrapper class as follows:

Apply above method for Boxing:

class PrimitiveToObject 
{
	public static void main(String[] args) 
	{
		byte x = 100;
		Byte y = Byte.valueOf(x);  // 'y' is Object type
		System.out.println("Object type : " + y);
	}
}

We can perform Boxing using Constructor also:

class PrimitiveToObject 
{
	public static void main(String[] args) 
	{
		byte x = 100;
		Byte y = new Byte(x);
		System.out.println("Object type : " + y);  
	}
}
Previous
Next

Add Comment

Courses Enquiry Form