Core Java - SPLessons

Wrapper Class in Java

Chapter 29

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Wrapper Class in Java

Wrapper Class in Java

shape Description

Wrapper Class in Java, The wrapper class is a process of converting the primitive into object and object into the primitive. Before wrapper class, autoboxing and unboxing feature used to convert primitive into object and object into primitive, the automatic conversion of primitive into an object is called as autoboxing. All wrapper classes are final. All wrapper classes are available in java.lang package.


shape Conceptual
figure

shape Wrapper
Class

Wrapper Class in Java, Following are the 8 wrapper classes.

Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double


shape Example

Wrapper Class in Java, Following is an example to convert from Primitive to Wrapper.

package splesson;

public class PrimitiveToWrapper {
	public static void main(String args[]){  
		//Converting int into Integer  
		int a=200;  
		Integer i=Integer.valueOf(a);//converting int into Integer  
		Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally  
		  
		System.out.println(a+" "+i+" "+j);  
		}
}

Converting int into Integer as follows.

int a=200;  
		Integer i=Integer.valueOf(a);

Output:
When compile the code following is the result will be displayed.

200 200 200

shape Example

Wrapper Class in Java, Following is an example to convert from Wrapper to Primitive.
WrappertoPrimitive.java

package splesson;

public class WrappertoPrimitive {
	public static void main(String args[]){    
		//Converting Integer to int    
		Integer a=new Integer(6);    
		int i=a.intValue();//converting Integer to int  
		int j=a;//unboxing, now compiler will write a.intValue() internally    
		    
		System.out.println(a+" "+i+" "+j);    
		}
}

The following is an image that shows how to do auto boxing and unboxing.

Output:
When compile the code following is the result will be displayed.

6 6 6

Call By Value In Java

shape Description

If programmer call a method passing by a value, then it is called as call by value. The following is an example where the original will not change.

package com;

public class MyOperation {
	 int data=100;  
	  
	 void change(int data){  
	 data=data+100;//changes will be in the local variable only  
	 }  
	     
	 public static void main(String args[]){  
		 MyOperation op=new MyOperation();  
	  
	   System.out.println("before change "+op.data);  
	   op.change(500);  
	   System.out.println("after change "+op.data);  
	  
	 }  
}

In the above example, the changes (data=data+100) are stored in local variable only. Now the result will be as follows.

before change 100
after change 100

The following is the same example where the original value will be changed.

package com;

public class MyOperation {
	int data=100;  
	  
	 void change(MyOperation op){  
	 op.data=op.data+100;//changes will be in the instance variable  
	 }  
	     
	    
	 public static void main(String args[]){  
		 MyOperation op=new MyOperation();  
	  
	   System.out.println("before change "+op.data);  
	   op.change(op);//passing object  
	   System.out.println("after change "+op.data);  
	  
	 }  
}

In the above example, programmer made the changes in called method, programmer passed object in area of primitive value. The result will be as follows.

before change 100
after change 200

For more detailed overview on methods click here


Summary

shape Key Points

  • Converting wrapper type object back to primitive type is known as Unboxing operation.
  • All wrapper classes are final.