Wrapper Classes in Java

The primitive data types in java are not objects. If we want to use these data types as objects, then we will have to use wrapper classes for each of these primitive data types provided in java.lang package.

There are many built-in classes, which cannot handle primitive data types as they deal only with objects. One such class is Vector, which is used to store a collection of objects. We cannot use the Vector class to directly store the collection of elements of a primitive data typed. But we can do so by storing the objects of wrapper classes, which correspond to the primitive data types.

The Java has wrapper class corresponding to each of the primitive data type as shown in the following table:

Primitive Data Types Wrapper Class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Number class:

The abstract class Number defines super-class that is implemented by the classes that wrap the numeric type byte, short, int, long, float, and double. Number class has abstract methods that return the value of the object in each of the different number formats. These methods are:

byte byteValue()

Returns the value of the specified number as a byte.

short shortValue()

Returns the value of the specified number as a short.

abstract int intValue()

Returns the value of the specified number as an int.

abstract long longValue()

Returns the value of the specified number as a long.

abstract float floatValue()

Returns the value of the specified number as a float.

abstract double doubleValue()

Returns the value of the specified number as a double.

Note-

The values returned by byteValue(), shortValue(), intValue(),longValue(), floatValue() and doubleValue() methods may involve rounding or truncation.

Example

The following example demonstrates how rounding and truncation takes place when invoking methods of class Number.

		class Wrapper
		{
		public static void main(String args[ ])
		{
		Integer iObj = new Integer(128);
		System.out.println(iObj.byteValue()); //truncation
		Long lObj = new Long(123456789123456789L);
		System.out.println(lObj);
		System.out.println(lObj.doubleValue());
		Float fObj = new Float(3.99f);
		System.out.println(fObj.intValue()); truncation
		}
		}
	
Output

-128

123456789123456789

1.23456789123456784E17

3

Converting Primitive Numbers to Objects using Constructors of Wrappers Classes and Converting Numeric Objects back to Primitive Numbers:-

Example

The following example demonstrates how primitives can be wrapped in objects and how they can be converted back to primitives.

		class Convert
		{
		public static void main(String args[])
		{
		System.out.println(“Converting primitive numbers to objects “ + “using constructors”);
		byte b = 105;
		Byte bObj = new Byte(b);
		System.out.println(bObj); //toString()
		short s = 2015;
		Short sObj = new Short(s)
		System.out.println(sObj);
		int i=32717;
		Integer iObj = new Integer(i);
		System.out.println(iObj);
		long l = 234543335565675L
		Long lObj = new Long(l);
		System.out.println(lObj);
		float f = 3.1415f;
		Float fObj = new Float(f);
		System.out.println(fObj);
		double d = 3.1415;
		Double dObj = new Double(d);
		System.out.println(dObj);
		System.out.println(“Converting numeric objects to primitive numbers”);
		byte b1 = bObj.byteValue();
		short s1 = sObj.shortValue();
		int i1 = iObj.intValue();
		long l1 = lObj.longValue();
		float f1 = fObj.floatValue();
		double d1 = dObj.doubleValue();
		System.out.println(b1);
		System.out.println(s1);
		System.out.println(i1);
		System.out.println(l1);
		System.out.println(f1);
		System.out.println(d1);
		}
		}
	
Output:

Converting primitive numbers to objects using constructor

105

2015

32717

234543335565675

3.1415

3.1415

Converting object to primitive numbers

105

2015

32717

234543335565675

3.1415

3.1415

Converting Primitive Numbers to Strings using toString() static method of the corresponding Wrapper Class

Example
		class ConvertPrimitiveToString
		{
		public static void main(String args[ ])
		{
		System.out.println(“Converting primitive numbers to String “ +
		 “using toString() static method of corresponding wrapper class:”);
		byte b = 105;
		String str=Byte.toString(b);
		System.out.println(str);
		short s=303;
		str = Short.toString(s);
		System.out.println(str);
		int i=100;
		str = Integer.toString(i);
		System.out.println(str);
		long l=454444444444l;
		str = Long.toString(l);
		System.out.println(str);
		float f=3.444f;
		str=Float.toString(f);
		System.out.println(str);
		double d=3.44444;
		str=Double.toString(d);
		System.out.println(str);
		}
		}
	
Output

Converting primitive numbers to String using toString() static method of

corresponding wrapper class:

105

303

100

454444444444

3.444

3.44444

Converting Numeric Objects to Strings using toString() method of the corresponding Wrapper Class:-
Example
		class ObjectToStringDemo
		{
		public static void main(String args[ ])
		{
		System.out.println(“Converting object numbers to Strings using” +
		“toString() method of corresponding wrapper class:”);
		byte b=103;
		Byte bObj = new Byte(b);
		String str=bObj.toString();
		System.out.println(str);
		short s=203;
		Short sObj=new Short(s);
		str = sObj.toString();
		System.out.println(str);
		Integer iObj = new Integer(32000);
		str = iObj.toString();
		System.out.println(str);
		str = new Long(4544444444444l).toString();
		System.out.println(str);
		str = new Float(3.1444f).toString();
		System.out.println(str);
		str = new Double(4.1444).toString();
		System.out.println(str);
		}
		}
	
Output:

Converting object numbers to Strings using toString() method of corresponding

wrapper class:

103

203

32000

4544444444444

3.1444

4.1444

Converting String Objects(Numeric Strings) to Numberic Objects using the static valueOf() method of the corresponding Wrapper Class
Example
		class StringToNumericObjectDemo
		{
		public static void main(String args[])
		{
		String str=”30”;
		String str2=”30.333”;
		Byte bObj=Byte.valueOf(str);
		System.out.println(bObj);
		//Byte bObj1 = new Byte(str2); //NumberFormatException
		Short sObj = Short.valueOf(str);
		System.out.println(sObj);
		Integer iObj=Integer.valueOf(str);
		System.out.println(iObj);
		Long lObj=Long.valueOf(“344324232432”);
		System.out.println(lObj);
		Float fObj=Float.valueOf(“3.333”);
		System.out.println(fObj);
		Double dObj=Double.valueOf(str2);
		System.out.println(dObj);
		}
		}
	
Output:

30

30

30

3.44324232432

3.33

30.333

Note:-

All of the valueOf() methods throw “NumberFormatException” if the string does not contain a parsable number.

Converting string Objects (Numeric Strings) to Numeric Objects using Constructor of the corresponding Wrapper Class
Example
		class StringToNumericObjectDemo1
		{
		public static void main(String args[])
		{
		String str=new String(“30”);
		//String str=”30”;
		String str2=new String(“30.333”);
		Byte bObj = new Byte(str);
		System.out.println(bObj);
		//Byte bObj=new Byte(str2); //NumberFormatException
		Short sObj=new Short(str);
		System.out.println(sObj);
		Integer iObj=new Integer(str);
		System.out.println(iObj);
		Long lObj=new Long(str);
		System.out.println(lObj);
		Float fObj=new Float(str);
		System.out.println(fObj);
		Double dObj=new Double(str);
		System.out.println(dObj);
		}
		}
	
Output:

30

30

30

30

30.333

30.333

Note:-

The Above constructor throw “NumberFormatException” if the string does not contain a parsable number.

Converting String Objects (Numeric Strings) to Primitive Numbers using parsing methods of the corresponding Wrapper Class
Example
		class StringToPrimitiveDemo
		{
		public static void main(String args[])
		{
		String str = new String(“30”);
		//String str=”30”;
		String str2= new String(“30.333”);
		byte b = Byte.parseByte(str);
		System.out.println(b);
		//byte b1=Byte.parseByte(str2); //NumberFormatException
		short s = Short.parseShort(str);
		System.out.println(s);
		int i = Integer.parseInt(str);
		System.out.println(i);
		long l = Long.parseLong(str);
		System.out.println(l);
		float f = Float.parseFloat(str2);
		System.out.println(f);
		double d = Double.parseDouble(str2);
		System.out.println(d);
		}
		}
	
Output:

30

30

30

30

30.333

30.333

Note:-

parseXXXXX() methods throw “NumberFormatException” if the string does not contain a parsable number.

Copyright 1999- Ducat Creative, All rights reserved.