Sunday, December 18, 2011

Wrapper Classes in Java

Java has for each primitive type a corresponding class that can be used to create objects according to the primitive type's value. These are called wrapper classes. In the table below, you can see the correspondence each primitive type and its wrapper class:

Primitive data type
Wrapper class
byte
Byte
short
Short
int
Integer
long
long
float
Float
double
Double
boolean
Boolean
char
Character

To create a wrapper class, one can use four methods.
  Byte b1 = new Byte("15");        //String constructor
  Byte b2 = new Byte((byte) 15);   //byte constructor
  Byte b3 = (byte)15;              //automatic cast, returns a byte type
  Byte b4 = Byte.parseByte("15");  //automatic cast, return a byte type
  Byte b5 = Byte.valueOf((byte)15);//returns a Byte type (wrapper)
  Byte b6 = Byte.valueOf("15");    //return a Byte type (wrapper)
The first method is by using a String as a parameter where the constructor will try to parse the data. If the String does not contain a valid number, a NumberFormatException will be thrown. The second method is by using a byte value as an argument for the constructor.

The third and fourth methods were made available starting with JDK 1.5 (that permitted automatic boxing/unboxing a.k.a automatic conversions from the primitive type to the wrapper type) and are similar with the first and second method. They return a byte type (primitive).

The fifth and sixth methods are very similar to the third and forth with the exception that they return a Byte type (wrapper). They were mostly used with older JDKs (before 1.5), but aren't considered deprecated.

In the example above I used byte's wrapper class for exemplification (Byte). The methods explained above can be used with all wrapper classes by changing the arguments and type-casts.

Also, all wrapper classes have methods that allow conversion to most of the other primitive types through the methods: byteValue(), shortValue(), intValue(), longValue, floatValue(), doubleValue(). If you have read this carefully, you may had observed that there is no booleanValue() or charValue() method. In languages like C or C++ the true value and the false value were associated with the numeric values 1 respectively 0. Simply put, Java doesn't do that.
  Double d = new Double(23.94);
  System.out.println(d.intValue()   + "\n"  + 
         d.byteValue()  + "\n"  +
         d.floatValue() + "\n"  +
         d.longValue()  + "\n"  +
         d.shortValue() + "\n"  
         );
  //Output
  //23
  //23
  //23.94
  //23
  //23
As you can see in the example above, a real number (in our case a double) is converted to an integer value by truncating the digits after the decimal point (it does not approximate 23.94 to 24). Still, there is a pitfall with these conversions:
  Double d = new Double(8.5e110);
  System.out.println(d.intValue()   + "\n"  + 
         d.byteValue()  + "\n"  +
         d.floatValue() + "\n"  +
         d.longValue()  + "\n"  +
         d.shortValue() + "\n"  
         );
  //Output
  //2147483647
  //-1
  //Infinity
  //9223372036854775807
  //-1
As you can see, a really large real number is not converted correctly, so be careful when using it. Also this applies to really small numbers (as module not negative).

To obtain the value that the wrapper class contains as a string, you simply need to call the toString() method.
  Double d = new Double(8.5e110);
  String s = d.toString();
  //s = "8.5E110"
To compare two wrapper objects one must use the equals() method. For comparing a wrapper class to a valid (as in terms of correspondence) primitive type the == operator could be used  (using automatic unboxing made available after JDK 1.5).
 Double d1 = new Double(5.10);
  Double d2 = new Double(5.10);
  double d3 = 5.10;
  if(d1.equals(d2))
   System.out.println("True");
  else
   System.out.println("False");
  if(d1==d3)
   System.out.println("True");
  else
   System.out.println("False");
  //Output
  //True
  //True
Also, one must note that is incorrect to compare two reference types (in our case objects of the class Double) using the == operators. This operator compares the memory addresses in this case and not the content of the wrapper.

The Double and Float wrapper classes also provide methods like isNaN(double) or isInfinite(double) that allow you to verify if a variable is valid (is not the result of 0/0) or if it is finite (is not the result of x/0, where x can be any number except 0).
if(Double.isInfinite(d1))
  System.out.println("The value is not a number!");
else
  System.out.println("The value is a number");
if(Double.isInfinite(d2))
  System.out.println("The value is infinite!");
else
  System.out.println("The value is finite");
Wrapper classes also provide methods for base conversionIEE754 representation,  determining the size of a type (in bytes) and the minimum/maximum values that a type could take, etc.

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger