Java Tutorial – Java Wrappers

Java Tutorial – Java Wrappers

In this tutorial we will discuss, in depth, the Java Wrapper classes used to represent each of the eight primitive data types (byte, short, int, long, float, double, char and boolean) in Java. These wrapper classes are immutable thus making them thread-safe.

What’s Covered

  1. What are Java Wrappers?
  2. When would I use Java Wrappers?
  3. Primitive Java Wrappers
  4. Integer Class Wrapper
  5. Float Class Wrapper
  6. Double Class Wrapper
  7. Boolean Class Wrapper

What are Java Wrappers?

The creators of Java realized that there will be times when we must use the classes representing their eight primitive data type equivalents. These classes are referred to as Wrapper Classes. These eight primitives are used to improve performance but there are times when primitive to object conversions are necessary. One common example that comes to mind is when using the java.util.Collection classes discussed in detail in the Java Collections Tutorial.

When would I use Java Wrappers?

As mentioned earlier, we use Java wrappers to wrap up primitive values into their object equivalents for use in collections like (ArrayList, HashMap, HashSet, TreeMap, etc). Prior to JDK 5, we were required to wrap our primitives prior to usage in our collections Boxing is the process of converting a primitive type to the corresponding reference type, such as byte to Byte. Unboxing is the reverse process, that is, conversion of the reference type to a primitive type, such as Integer to int.

List list = new ArrayList();
list.add(new Integer(23));
list.add(new Integer(9));
list.add(new Integer(15));
		
for (int i=0; i < list.size(); i++) {
  int val = ((Integer) list.get(i)).intValue();
  System.out.println("ArrayList Value..: " + val);
}

With the advent of JDK 1.5, support for Generics, Auto-boxing has been added. As you can see from the example below, this make it much easier to add elements into the arraylist and get elements from the arraylist as we do not need to concern ourselves with ensuring the elements are objects. We are able to use the primitives directly.

List<Integer> list = new ArrayList<Integer>();
list.add(23);
list.add(9);
list.add(15);
		
for (int val: list) {
  System.out.println("ArrayList Value..: " + val);
}

Primitive Java Wrappers

All of the numeric wrapper classes are subclasses of the abstract class Number. The abstract Number class contains methods byteValue(), doubleValue(), floatValue(), intValue(), longValue() and shortValue(). Therefore all classes derived from the Number class must implement these methods.

java_wrapper_hierarchy

The package java.lang contains several classes that function as the wrappers for the primitives. These are Byte, Short, Integer, Long, Float, Double, Character, and Boolean.

Integer Class Wrapper

The Integer class wraps the int primitive. This class contains several methods for converting the integer value to the primitive equivalents using byteValue(), doubleValue(), floatValue(), intValue(), longValue() and shortValue().

short sQty = inStock.shortValue();
int iQty = inStock.intValue();
long lQty = inStock.longValue();

System.out.println("short sQty..: " + sQty);
System.out.println("short iQty..: " + iQty);
System.out.println("short lQty..: " + lQty);

Integer Constructor

The Integer class has the following two constructors:

Integer(int value)
 Integer(String s)

Let’s illustrate using these constructors with the following example:

Integer inStock = new Integer(139);
Integer onOrder = new Integer("18");

System.out.println("inStock..: " + inStock);
System.out.println("onOrder..: " + onOrder);

Integer Constructor Output

inStock..: 139
onOrder..: 18

This class also has static methods that convert int to String using toString(int i) and for parsing a String to an int parseInt(String s).

int parseInt(String s)
 String toString(int i)

In this example we parse a String with a value of “782” to an int value and stores that in a variable num. Additionally, we convert an int value of 342 to the String representation.

int num = Integer.parseInt("782");
String strNum = Integer.toString(342);

System.out.println("num.......: " + num);
System.out.println("strNum....: " + strNum);

Float Class Wrapper

The Float class is a wrapper class with the value of primitive type float in a Java object. Just like the Integer class described above, the Float class contains several static methods for converting the float value to the primitive equivalents using byteValue(), doubleValue(), floatValue(), intValue(), longValue() and shortValue().

Float Constructor

The Float class has the following two constructors:

Float(float value)
 Float(double value)
 Float(String s)

Let’s illustrate using these constructors with the following example:

Float unitPrice = new Float(39.99f);
Float salePrice = new Float("24.69f");

System.out.println("unitPrice..: " + unitPrice);
System.out.println("salePrice..: " + salePrice);

Float Constructor Output

unitPrice..: 39.99
salePrice..: 24.69

Double Class Wrapper

The Double class is a wrapper class with the value of primitive type double in a Java object. Just like the Integer and Float class described above, the Double class contains several static methods for converting the double value to the primitive equivalents using byteValue(), doubleValue(), floatValue(), intValue(), longValue() and shortValue().

Double Constructor

The Double class has the following two constructors:

Double(double value)
 Double(String s)

Let’s illustrate using these constructors with the following example:

Double totalAssets = new Double(1872920.83d);
Double totalLiabs= new Double("154030.31D");

System.out.println("totAsset......: " + totalAssets);
System.out.println("totLiability..: " + totalLiabs);

Double Constructor Output

totalAsset......: 1872920.83
totalLiability..: 154030.31

The remaining wrapper classes (Byte, Short and Long) derived from Number class all share the same methods therefore I will skip over these in this post.

Boolean Class Wrapper

The Boolean class is a wrapper class with the value of primitive type boolean in a Java object.

Boolean Constructor

The Boolean class has the following two constructors:

Boolean(boolean value)
 Boolean(String s)

Let’s illustrate using these constructors with the following example:

Boolean isActive = new Boolean(true);
Boolean isEmployee = new Boolean("false");
  
System.out.println("isActive...: " + isActive);
System.out.println("isEmployee.: " + isEmployee);

Boolean Constructor Output

isActive...: true
isEmployee.: false

Converting from Boolean to boolean

// Convert from Boolean Class to boolean
boolean active = isActive.booleanValue();
		
System.out.println("active.....: " + active);  

Output of Boolean to boolean conversion

active.....: true

Character Class Wrapper

The Character class is a wrapper class with the value of primitive type char in a Java object.

Character Constructor

The Character class has only one constructor:

Character(char value)

Let’s illustrate using these constructors with the following example:

Character ch = new Character('a');

System.out.println("ch.....: " + ch);

Character Constructor Output

ch.....: a

That’s It!

I hope you enjoyed this tutorial. It was certainly a lot of fun putting it together and testing it out. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!

java_wrappers

Core Java Related Tutorials

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *