Importance of Wrapper classes
There are mainly two uses with wrapper classes.
1) To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
2) To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
Features of the Java wrapper Classes.
1) Wrapper classes convert numeric strings into numeric values.
2) The way to store primitive data in an object.
3) The valueOf() method is available in all wrapper classes except Character
4) All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.
******PROGRAM TO DEMONSTRATE AUTO-BOXING/AUTO-UNBOXING TO PREVENT
ERRORS*****
class preventerror
{
public static void main(String args[])
{
Integer iOb = 1000; // autobox the value 1000
float i = iOb.floatValue(); // manually unbox as byte !!!
System.out.println(i); // does not display 1000 !
float i2=iOb;
System.out.println(i2); // does not display 1000 !
double id=iOb;
System.out.println(id); // does not display 1000 !
byte b = Byte.parseByte("25");
System.out.println(b);//Output : 25-byte
short s = Short.parseShort("45");
System.out.println(s);//Output : 45-short
int ii = Integer.parseInt("123");
System.out.println(ii);//Output : 123int x=iOb;
}
}
******PROGRAM ON BOXING/ UNBOXING *****
public class wrapperdemo
{
public static void main (String args[])
{
byte grade = 2;
int marks = 50;
float price = 8.6f;
double rate = 50.5;
char x='A';
boolean b=false;
String s="12345";
System.out.println("string object s: " + s);
// data types to objects
Boolean blnObj = new Boolean(b);
Character xchar = new Character(x);
Byte g1 = new Byte(grade); // wrapping
Integer m1 = new Integer(marks);
Float f1 = new Float(price);
Double r1 = new Double(rate);
Integer m2 = new Integer(s);
// let us print the values from objects
System.out.println("Values of Wrapper objects (printing as objects)");
System.out.println("boolean object blnObj: " + blnObj);
System.out.println("Byte object xchar: " + xchar);
System.out.println("Byte object g1: " + g1);
System.out.println("Integer object m1: " + m1);
System.out.println("Float object f1: " + f1);
System.out.println("Double object r1: " + r1);
System.out.println("Integer object m2: " + m2);
//objects to data types (retrieving data types from objects)
char c1 = xchar.charValue();
boolean q = blnObj.booleanValue();
byte bv = g1.byteValue(); // unwrapping
int iv = m1.intValue();
float fv = f1.floatValue();
double dv = r1.doubleValue();
int iv1 = m2.intValue();
if(s == iv1)
System.out.println("both the strings are equal");
if(iv1 == m2)
System.out.println("both the strings are equal");
// let us print the values from data types
System.out.println("Unwrapped values (printing as data types)");
System.out.println("xchar value: " + c1); // prints x
System.out.println(b);
System.out.println("byte value, bv: " + bv);
System.out.println("int value, iv: " + iv);
System.out.println("float value, fv: " + fv);
System.out.println("double value, dv: " + dv);
System.out.println("int value, iv1: " + iv1);
}
******PROGRAMS ENUMERATION CONCEPTS *****
enum enum1
{
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class EnumDemo
{
public static void main(String args[])
{
enum1 ap;
ap = enum1.RedDel;
// Output an enum value.
System.out.println("Value of ap: " + ap);
System.out.println();
ap = enum1.GoldenDel;
// Compare two enum values.
if(ap == enum1.GoldenDel)
System.out.println("ap contains GoldenDel.\n");
// Use an enum to control a switch statement.
switch(ap)
{
case Jonathan:
System.out.println("Jonathan is red.");
break;
case GoldenDel:
System.out.println("Golden Delicious is yellow.");
break;
case RedDel:
System.out.println("Red Delicious is red.");
break;
case Winesap:
System.out.println("Winesap is red.");
break;
case Cortland:
System.out.println("Cortland is red.");
break;
}
}
}
*****Enumeration with constructor, overridden constructor instance
variable, and methods*****
enum Apple {
Jonathan(10), GoldenDel(9), RedDel, Winesap(15), Cortland(8);
private int price; // price of each apple
// Constructor
Apple(int p)
{
price = p;
}
//Overloaded constructor
Apple()
{
price = -1;
}
int getPrice()
{
return price;
}
}
class enumdemo3
{
public static void main(String args[])
{
Apple ap;
//Display price of Winesap.
System.out.println("Winesap costs " +Apple.Winesap.getPrice() +" cents.\n");
//Display all apples and prices.
System.out.println("All apple prices:");
for(Apple a : Apple.values())
System.out.println(a + " costs " + a.getPrice() +" cents.");
}
}
*****Demonstrate valueOf(),values(),ordinal(), compareTo(), and
equals().******
// An enumeration of apple varieties.
enum Appletypes
{
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class enummethods
{
public static void main(String args[])
{
Appletypes ap, ap2, ap3;
// Demonstrate valueOf() and values() methods
System.out.println(Appletypes.valueOf("RedDel"));
// Obtain all ordinal values using ordinal().
System.out.println("Here are all apple constants" + " and their ordinal
values: ");
for(Appletypes a : Appletypes .values())
System.out.println(a + " " + a.ordinal());
ap = Appletypes .RedDel;
ap2 = Appletypes .GoldenDel;
ap3 = Appletypes .RedDel;
System.out.println();
// Demonstrate compareTo() and equals()
if(ap.compareTo(ap2) < 0)
System.out.println(ap + " comes before " + ap2);
if(ap.compareTo(ap2) > 0)
System.out.println(ap2 + " comes before " + ap);
if(ap.compareTo(ap3) == 0)
System.out.println(ap + " equals " + ap3);
if(ap.equals(ap2))
System.out.println("Error!");
if(ap.equals(ap3))
System.out.println(ap + " equals " + ap3);
if(ap == ap3)
System.out.println("ap not equal to ap3");
}
}
*****Demonstrate difference between equals() and == operator******
public class equal
{
public static void main(String[] args)
{
String s1 = new String("HELLO");
String s2 = new String("HELLO");
String s3=s1;
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s1 == s3);
}
}