Wrapper class in Java
If we use primitive data types in java programs that is not a fully object oriented, so introduced wrapper classes in java. The wrapper class mechanism is converting primitive type to object and object type to primitive type. The java.lang package is a wrapper class in java.
The eight wrapper classes are,
Boolean ------ boolean
Character ------- char
Byte ------- byte
Short ------- short
Integer ------- int
Long ------- long
Float ------- float
Double ------- double
Auto-boxing:
If we converting primitive data type to object is called as auto-boxing.
Example:
int x=5;
Integer ob = new Integer(x);
Auto-unboxing:
Converting reference to primitive data type is known as auto-unboxing.
Example:
Integer ob = new Integer(x);
int x=5;
Sample program:
public class BoxingVariables {
public static void main(String []args){
int x=5;
Integer ob = new Integer(x); //converting int to Integer
System.out.println(ob);
Integer a=new Integer(15);
int i=a.intValue(); //converting Integer to int
System.out.println(+i);
}
}
Interview Questions:
Explain about Wrapper classes in Java?
What is Auto-boxing in java?
What is Auto-unboxing in java?
Write a sample program to convert the reference to an object?
List out all the wrapper classes in Java?
Why we are using Wrapper classes?
In which package wrapper classes are present?
EmoticonEmoticon