ZOFTINO.COM android and web dev tutorials

Java Primitive Wrapper Classes

Java provides wrapper classes which represent primitive data types. Primitive data type wrapper classes allow primitive data types to be used with collections. Wrapper classes provide methods which can be used to convert one type to another type. Wrapper classes are defined in java.lang package.

Number

Number class is the base class for all wrapper classes which contain values of numerical primitive data types such as short, byte, int, long, double and float. Number class is an abstract class so it can’t be instantiated.

Number class contains methods such as byteValue(), shortValue(), intValue(), longValue(), floatValue() and doubleValue(), which the wrapper classes implement to provide methods for converting the value the wrapper class contains to other numerical primitive types.

Integer

Integer, which is a subclass of Number class, wraps the value of primitive data type int. Integer class contains methods and constants which are useful for handling int.

Here is how an int value can be assigned to Integer.

		//asign integer to Integer variable
		Integer i = 3;	
		//Integer instance passing int
		Integer x = new Integer(3);	
		//Integer instance passing string
		Integer y = new Integer("3");

To use int type with collection classes such as ArrayList and Set, you need to use Integer as shown in the example.

		List<Integer> scoreList = new ArrayList<Integer>();
		scoreList.add(45);
		scoreList.add(99);
		scoreList.add(33);
		scoreList.add(39);
		
		System.out.println("before sort : "+scoreList.toString());
		
		scoreList.sort(Comparator.naturalOrder());
		
		System.out.println("after sort : "+scoreList.toString());

Constants

Integer has constants such as TYPE, MAX_VALUE, MIN_VALUE, SIZE and BYTES which give such info about int as max and min values and number bytes it takes to store int value.

		System.out.println("Integer.TYPE : "+Integer.TYPE);
		System.out.println("Integer.MAX_VALUE : "+Integer.MAX_VALUE);
		System.out.println("Integer.MIN_VALUE : "+Integer.MIN_VALUE);
		System.out.println("Integer.SIZE : "+Integer.SIZE);
		System.out.println("Integer.BYTES : "+Integer.BYTES);

Output :

Integer.MAX_VALUE : 2147483647
Integer.MIN_VALUE : -2147483648
Integer.SIZE : 32
Integer.BYTES : 4

Converting Integer to different types

You can use methods such as byteValue(), shortValue(), intValue(), longValue(), floatValue() and doubleValue() to convert integer to different types.

	Integer bestVal = 678;	
		byte b = bestVal.byteValue();
		short s = bestVal.shortValue();
		float f = bestVal.floatValue();
		double d = bestVal.doubleValue();
		long l = bestVal.longValue();

Useful methods

To convert String to Integer, use valueOf() method.

Integer val = Integer.valueOf("34");

For two numbers comparison, you can use compare(), max() and min() methods.

System.out.println("Integer.min : "+Integer.min(250,  350));		
System.out.println("Integer.max : "+Integer.max(250,  350));		
System.out.println("Integer.compare : "+Integer.compare(250, 350));

Output:

Integer.min : 250
Integer.max : 350
Integer.compare : -1

Other Wrapper Classes

Like Integer, Java provides Byte, Short, Long, Float and Double wrapper classes for other numerical primitive data types. Similar to Integer, all the wrapper class related to numbers have constants and methods which can be used to convert value from one type to different type.

Similarly, Boolean wrapper class contains boolean value and Character wrapper class contain char value.

Character someChar = 'a';

Some of the important methods of Character class are isDigit, isLetter, isLowerCase, isUpperCase, isSpaceChar, isWhiteSpace, toLowerCase and toUpperCase.

if(Character.isDigit('3')) {
	System.out.println("it is digit");
}

Autoboxing

You can assign value of primitive data type variable to corresponding wrapper class variable without needing to instantiate the wrapper class objects. This automatic conversion of primitive type to wrapper class type is known as autoboxing.

		int intVal = 22;
		Integer intValObj = intVal;
		
		float floatVal = 34;
		Float floatValObj = floatVal;
		
		double doubleVal = 55.44;
		Double doubleValObje = doubleVal;

Unboxing

You can assign wrapper object to corresponding primitive data type variable. Java automatically converts the types and this automatic conversion of wrapper object to corresponding primitive data type is called unboxing.

Boolean bValue = new Boolean(true);
boolean c = bValue;