Advertisement
//Java - Calling the construtor of wrapper classes
//to box a primitive value in its equivalent wrapper class object.
class A
{
public static void main(String... ar)
{
//Converting an int to Integer object.
Integer in= new Integer(10);
//Converting a char to Character object.
Character c = new Character('h');
//Converting a float to Float object.
Float f= new Float("10.2");
//Converting a double to Double object.
Double d= new Double("22.2");
}
}
//Java - Autoboxing a primitive to its equivalent wrapper class object.
class A
{
public static void main(String... ar)
{
//Autoboxing a char to Character by direct assignment
Character ch = 'c';
//Autoboxing a boolean to Boolean by direct assignment
Boolean bool = true;
//Autoboxing a byte to Byte by assigning
Byte byte = 100;
//Autoboxing a short to Short by assigning
Short sh = 10;
//An int is boxed into an Integer object.
Integer i = 10;
//Adding l or L is neccessary because by default every non-decimal numeric value is int.
Long l = 1000L;
//Adding f or F is neccessary because by default every decimal value is double.
Float fl = 10.5f;
//Autoboxing a floating-point value to double.
Double db = 25.5;
}
}
Advertisement
//Java - Unboxing before Java 5
class A
{
public static void main(String... ar)
{
//Boxing
Character ch = new Character('c');
Boolean bool = new Boolean(true);
Byte byt = new Byte(100);
Short sh = new Short(10);
Integer intg = new Integer(10);
Long lg = new Long(1000l);
Float fl = new Float(10.5f);
Double db = new Double(25.5);
//Calling ---Value() method of each wrapper class to unbox the object to its primitive value.
char c= ch.charValue();
boolean b= bool.booleanValue();
byte bt = byt.byteValue();
short s= sh.shortValue();
int i= intg.intValue();
long lo= lg.longValue();
float f= fl.floatValue();
double d= db.doubleValue();
System.out.println("Unboxed primitive value of Character object : " + c);
System.out.println("Unboxed primitive value of Boolean object : " + b);
System.out.println("Unboxed primitive value of Byte object : " + bt);
System.out.println("Unboxed primitive value of Short object : " + s);
System.out.println("Unboxed primitive value of Integer object : " + i);
System.out.println("Unboxed primitive value of long object : " + lo);
System.out.println("Unboxed primitive value of Float object : " + f);
System.out.println("Unboxed primitive value of Double object : " + db);
}
}
Unboxed primitive value of Character object : c
Unboxed primitive value of Boolean object : true
Unboxed primitive value of Byte object : 100
Unboxed primitive value of Short object : 10
Unboxed primitive value of Integer object : 10
Unboxed primitive value of long object : 1000
Unboxed primitive value of Float object : 10.5
Unboxed primitive value of Double object : 25.5
//Java - Autoboxing a primitive to its wrapper class object, directly.
class A
{
public static void main(String... ar)
{
//Autoboxing
Character ch = 'c';
Boolean boo = true;
Byte byt = 100;
Short sh = 10;
Integer intg = 10;
Long lg = 1000l;
Float fl = 10.5f;
Double db = 25.5;
//Auto-unboxing
char c= ch; //direct unboxing a Character object to a primitive char.
boolean b= boo; //direct unboxing a Boolean object to a primitive boolean.
byte bt = byt; //direct unboxing a Byte object to a primitive byte.
short s= sh; //direct unboxing a Short object to a primitive short.
int i= intg; //direct unboxing a Integer object to a primitive int.
long lo= lg; //direct unboxing a Long object to a primitive long.
float f= fl; //direct unboxing a Float object to a primitive float.
double d= db; //direct unboxing a Double object to a primitive double.
System.out.println("Unboxed primitive value of Character object " + c);
System.out.println("Unboxed primitive value of Boolean object " + b);
System.out.println("Unboxed primitive value of Byte object " + bt);
System.out.println("Unboxed primitive value of Short object " + s);
System.out.println("Unboxed primitive value of Integer object " + i);
System.out.println("Unboxed primitive value of long object " + lo);
System.out.println("Unboxed primitive value of Float object " + f);
System.out.println("Unboxed primitive value of Double object " + db);
}
}
Unboxed primitive value of Character object c
Unboxed primitive value of Boolean object true
Unboxed primitive value of Byte object 100
Unboxed primitive value of Short object 10
Unboxed primitive value of Integer object 10
Unboxed primitive value of long object 1000
Unboxed primitive value of Float object 10.5
Unboxed primitive value of Double object 25.5
//Java - Autoboxing and unboxing when calling a method
class A
{
public void boxUnbox(Integer a) //Primitive value being passed is autoboxed to an Integer object
{
//Auto-unbox, Increment and Auto-box
++a;
//Auto-unboxing of Integer to int.
int c = a;
System.out.println(c);
}
public static void main(String... ar)
{
int a = 10;
A ob = new A();
//Passing primitive int value to a method that accepts an Integer object argument.
ob.method(a);
}
}
11
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement