how2examples.com
Home - Introduction to Java Programming

Java Wrapper Classes

Every primitive type has a corresponding wrapper class. The wrapper classes provide a way to wrap a primitive type in an object. Wrapper classes have the advantage of being able to be used where primitive types cannot, such as being stored in collections. The wrapper classes also provide static utility methods for converting primitive types to and from String instances.

PrimitiveWrapper Class
booleanjava.lang.Boolean
bytejava.lang.Byte
charjava.lang.Character
doublejava.lang.Double
floatjava.lang.Float
intjava.lang.Integer
longjava.lang.Long
shortjava.lang.Short

Constructing Wrapper objects

Every wrapper class, except Character, has two constructors that each accept a single argument. One accepts an argument of the primitive type the wrapper class represents and the other accepts a String representation of the primitive value. The Character has a single constructor that accepts a single char argument.

Converting a primitive to a wrapper

Every wrapper class has a public static valueOf(primitive) method that returns an instance of the wrapper with a value represented by the specified corresponding primitive argument.

Converting a String to a wrapper.
Code snippet:
Boolean bool = Boolean.valueOf(true);
Byte b = Byte.valueOf((byte) 127);
Character c = Character.valueOf('a');
Short s = Short.valueOf((short) -32768);
Integer i = Integer.valueOf(2147483647);
Long l = Long.valueOf(-9223372036854775808L);
Float f = Float.valueOf(3.4028235E38F);
Double d = Double.valueOf(1.33334533);
System.out.println(bool+" "+b+" "+c+" "+s+" "+i+" "+l+" "+f+" "+d);
Output from running code snippet:
true 127 a -32768 2147483647 -9223372036854775808 3.4028235E38 1.33334533

Converting a wrapper to a primitive

The Byte, Double, Float, Integer, Long and Short all have methods like xxxValue(), where xxx is the name of the primitive type the method returns. The xxxValue() methods return the value of the wrapper object as the primitive type indicated by the method name. When a conversion from a wrapper to a primitive requires a narrowing conversion there can be a loss of precision.

WrapperToPrimitive.
Code snippet:
Double w = 669.32;
byte b = w.byteValue();
short s = w.shortValue();
int i = w.intValue();
long l = w.longValue();
float f = w.floatValue();
double d = w.doubleValue();
System.out.println(b+" "+s+" "+i+" "+l+" "+f+" "+d);
Output from running code snippet:
-99 669 669 669 669.32 669.32

Converting a String to a primitive

Every wrapper class, except Character, has a method like public static parseXxx(String s), where Xxx is the name of the wrapper class. For example, Integer has a public static int parseInt(String s) method and Boolean has a public static boolean parseBoolean(String s) method. The methods return, as a primitive type, the value represented by the specified String argument.

Converting a String to a primitive.
Code snippet:
boolean bool = Boolean.parseBoolean("true");
byte b = Byte.parseByte("127");
short s = Short.parseShort("-32768");
int i = Integer.parseInt("2147483647");
long l = Long.parseLong("-9223372036854775808");
float f = Float.parseFloat("3.4028235E38");
double d = Double.parseDouble("1.33334533");
System.out.println(bool+" "+b+" "+s+" "+i+" "+l+" "+f+" "+d);
Output from running code snippet:
true 127 -32768 2147483647 -9223372036854775808 3.4028235E38 1.33334533

A NumberFormatException is thrown if the value represented by the String parameter cannot be converted to the required primitive type.

NumberFormatException while attempting to convert a String to a primitive.
Code snippet:
try {
   Byte.parseByte("128");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Short.parseShort("32768");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Integer.parseInt("one");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Long.parseLong("1.234");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Float.parseFloat("hj");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Double.parseDouble("hj");
} catch (NumberFormatException e) {
   System.out.println(e);
}
Output from running code snippet:
java.lang.NumberFormatException: Value out of range. Value:"128" Radix:10
java.lang.NumberFormatException: Value out of range. Value:"32768" Radix:10
java.lang.NumberFormatException: For input string: "one"
java.lang.NumberFormatException: For input string: "1.234"
java.lang.NumberFormatException: For input string: "hj"
java.lang.NumberFormatException: For input string: "hj"

The Byte, Short, Integer and Long classes also have a method like public static parseXxx(String s, int radix), where Xxx is the name of the wrapper class, that accepts a second parameter specifying the radix used in the string representation.

Specifying a radix to use when converting a String to a primitive.
Code snippet:
byte binary = Byte.parseByte("1101", 2);
short octal = Short.parseShort("15", 8);
int decimal = Integer.parseInt("13", 10);
long hexadecimal = Long.parseLong("d", 16);
System.out.println(binary+" "+octal+" "+decimal+" "+hexadecimal);
Output from running code snippet:
13 13 13 13

Converting a String to a wrapper

Every wrapper class, except Character, has a public static valueOf(String) method that returns an instance of the wrapper with a value represented by the specified String argument.

Converting a String to a wrapper.
Code snippet:
Byte b = Byte.valueOf("127");
Short s = Short.valueOf("-32768");
Integer i = Integer.valueOf("2147483647");
Long l = Long.valueOf("-9223372036854775808");
Float f = Float.valueOf("3.4028235E38");
Double d = Double.valueOf("1.33334533");
System.out.println(b+" "+s+" "+i+" "+l+" "+f+" "+d);
Output from running code snippet:
127 -32768 2147483647 -9223372036854775808 3.4028235E38 1.33334533

A NumberFormatException is thrown if the value represented by the String parameter cannot be used to construct a new instance of the wrapper class.

NumberFormatException while attempting to convert a String to a wrapper.
Code snippet:
try {
   Byte.valueOf("128");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Short.valueOf("32768");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Integer.valueOf("one");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Long.valueOf("1.234");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Float.valueOf("hj");
} catch (NumberFormatException e) {
   System.out.println(e);
}
try {
   Double.valueOf("hj");
} catch (NumberFormatException e) {
   System.out.println(e);
}
Output from running code snippet:
java.lang.NumberFormatException: Value out of range. Value:"128" Radix:10
java.lang.NumberFormatException: Value out of range. Value:"32768" Radix:10
java.lang.NumberFormatException: For input string: "one"
java.lang.NumberFormatException: For input string: "1.234"
java.lang.NumberFormatException: For input string: "hj"
java.lang.NumberFormatException: For input string: "hj"

Unlike the valueOf(String) methods of the other wrapper classes, Boolean.valueOf(String) does not throw a NumberFormatException. It returns true if the String argument is equal, ignoring case, to the string "true" - else it returns false.

Converting a String to a Boolean.
Code snippet:
Boolean b1 = Boolean.valueOf("true");
Boolean b2 = Boolean.valueOf("TRUE");
Boolean b3 = Boolean.valueOf("false");
Boolean b4 = Boolean.valueOf("yes");
System.out.println(b1+" "+b2+" "+b3+" "+b4);
Output from running code snippet:
true true false false

Converting a primitive to a String

Every wrapper class has a public static String toString(primitive) method that accepts, as a single argument, the primitive type the wrapper class represents. The methods return a String object representing the specified primitive.

Converting a primitive to a String.
Code snippet:
String s = Integer.toString(998);
System.out.println(s);
Output from running code snippet:
998

The Integer and Long classes also have a public static String toString(primitive, int radix) method thet, as well as accepting a primitive type to be converted to a String, also accepts a second parameter specifying the radix to use in the string representation.

Specifying a radix to use when converting a primitive to a String.
Code snippet:
int i = 13;
String binary = Integer.toString(i, 2);
String octal = Integer.toString(i, 8);
String decimal = Integer.toString(i, 10);
String hexadecimal = Integer.toString(i, 16);
System.out.println(binary+" "+octal+" "+decimal+" "+hexadecimal);
Output from running code snippet:
1101 15 13 d

Converting a wrapper to a String

As with all java objects, wrapper instances have a public String toString() method. The value returned by the toString() method is a String representation of the wrapped primitive.

WrapperToString.
Code snippet:
String s = new Integer(-8735).toString();
System.out.println(s);
Output from running code snippet:
-8735