What is a wrapper class?
Every primitive data type in java has a dedicated class known as a wrapper class. The class is called as wrapper class because they wrap the primitive data type into an object of that class.Wrapper classes are part of java.lang package.All the methods of a wrapper class are static so they can be used without creating an instance of the wrapper class.
Converting wrapper class object to its corresponding primitive type and vice versa
1. Float wrapper class
Example: To convert a Float object to float primitive type.
public class Convert
{
public static void main(String [] args)
{
Float fObj = new Float(“5.5”);
float f = fObj.floatValue();
}
}
Example: To convert a float primitive type to a Float object .
public class Convert
{
public static void main(String [] args)
{
float f = 5.5;
Float fObj = new Float(f);
}
}
2. Integer wrapper class
Example: To convert an Integer object to integer primitive type.
public class Convert
{
public static void main(String [] args)
{
Integer intObj = new Integer(“5”);
int i = intObj.intValue();
}
}
Example: To convert an integer primitive type to a Integer object .
public class Convert
{
public static void main(String [] args)
{
int i = 5;
Integer intObj = new Integer(i);
}
}
3. Double wrapper class
Example: To convert a Double object to double primitive type.
public class Convert{
public static void main(String [] args)
{
Double dObj = new Integer(“10.5”);
double d = dObj.doubleValue();
}
}
Example: To convert a double primitive type to a Double object .
public class Convert
{
public static void main(String [] args)
{
double d = 10. 5;
Double dObj = new Double(d);
}
}
4. Boolean wrapper class
Example: To convert a Boolean object to boolean primitive type.
public class Convert
{
public static void main(String [] args)
{
Boolean bObj = new Boolean(“true”); // Construct new Boolean object
Boolean b = bObj .booleanValue(); // Use booleanValue of Boolean class to convert into boolean type
}
}
Example:To convert a boolean primitive type to a Boolean object .
public class Convert
{
public static void main(String [] args)
{
boolean b = true;
Boolean b1Obj = new Boolean(b); // Using constructor
Boolean b2Obj =Boolean.valueOf(b); // Using valueOf method which is a static method
}
}
5. Byte wrapper class
Example: To convert a Byte object to byte primitive type.
public class Convert
{
public static void main(String [] args)
{
Byte byObj = new Byte(“5”);
byte by = byObj.byteValue(); // uses byteValue method of Byte class to convert to primitive type
}
}
Example:To convert an byte primitive type to a Byte object .
public class Convert{
public static void main(String [] args)
{
byte by = 5;
Byte byObj = new Byte(by); // Uses constructor of Byte class to convert primitive type to object
}
}
6. Short wrapper class
Example: To convert a Short object to short primitive type.
public class Convert
{
public static void main(String [] args)
{
Short shObj = new Short (“5”);
short s = shObj.shortValue(); // uses shortValue method of Short class to convert to primitive type
}
}
Example: To convert a short primitive type to a Short object.
public class Convert{
public static void main(String [] args)
{
short s = 5;
Short shObj = new Short(s); // Uses constructor of Short class to convert primitive type to object
}
}
7. Long wrapper class
Example: To convert a Long object to long primitive type.
public class Convert{
public static void main(String [] args) {
Long lObj = new Long (“5”);
long l = lObj.longValue(); // uses longValue method of Long class to convert to primitive type
}
}
Example: To convert long primitive type to a Long object.
public class Convert
{
public static void main(String [] args)
{
long l = 5;
Long lObj = new Long (l); // Uses constructor of Long class to convert primitive type to object
}
}
8. Character wrapper class
Example:To convert a Character object to char primitive type.
public class Convert
{
public static void main(String [] args)
{
Character chObj = new Character (“abc”);
char s = chObj.charValue(); // uses charValue method of Character class to convert to primitive type
}
}
Example:To convert char data type to a Character object.
public class Convert{
public static void main(String [] args)
{
char ch = abc;
Character chObj = new Character (ch); // Uses constructor of Character class to convert primitive type to object
}
}
Leave a Reply