Monday, June 25, 2012

Foundation of Autoboxing - Part 1


The main purpose of AutoBoxing(unBoxing) is provide a way to work with primitives as Objects, in order we can use Generics and so on.

Find below the Wrapper classes:



Creating Wrapper Objects

Most of Wrappers provide a constructor that gets as an argument a primitive value and a String object. Only the Wrapper class Character gets one argument which is a char primitive.
Let's see some examples:

public class WrapperJava {
public static void main(String[] args) {
Integer n1 = Integer("10");
Integer n2 = Integer(10);
Float f1 = new Float("5.3");
Float f2 = new Float("5.3f");
Float f3 = new Float("5.3d");
Float f4 = new Float(5.3);
Float f5 = new Float(5.3f);
Character c1 = new Character('d');
//... so on
}
}
view raw gistfile1.java hosted with ❤ by GitHub

All the wrapper classes mentioned so far contain a method called valueOf(), this method is a static method and can get different arguments depending on the wrapper, for instance:

Byte/Short/Integer/Long

[Wrapper] static valueOf(primitive)
[Wrapper] static valueOf(String)
[Wrapper] static valueOf(primitive, int radix) // radix defines the base like decimal, octal or hexadecimal.

Float/Double/Boolean

[Wrapper] static valueOf(primitive)
[Wrapper] static valueOf(String)

Character
[Wrapper] static valueOf(primitive)

Conversion 

Also we can make a conversion using the wrapper classes by calling the methods xxxValue(), all these methods don't have arguments, for instance:

Wrapper classes
The following classes extend Number -> Byte/Double/Float/Integer/Long/Short



Slightly different from the valueOf() method, the static method parseXXX() has two "flavours" similar to valueOf(), furthermore these methods are applicable only for the 6(six) integer classes and not to mention that the return of these is a primitive type instead of a wrapper class.

[Wrapper] static valueOf(String)
[Wrapper] static valueOf(primitive, int radix) // radix defines the base like decimal, octal or hexadecimal.

Example of valueOf, xxxValue and parseXxx

package br.com.ocjp.autoboxing;
public class Examples {
public static void main(String[] args) {
// valueOf
Byte b1 = Byte.valueOf((byte)100);
Byte b2 = Byte.valueOf("101");
System.out.println(b1); // Prints 100
System.out.println(b2); // Prints 101
// parseXXX
byte b3 = Byte.parseByte("100");
byte b4 = Byte.parseByte("010", 8);
byte b5 = Byte.parseByte("F", 16);
System.out.println(b3); // Prints 100
System.out.println(b4); // Prints 8
System.out.println(b5); // Prints 15
// xxxValue
Integer i = Integer.valueOf("F",16);
byte b6 = i.byteValue();
System.out.println(b6); //Prints 15
// toString /toString(xxx) and toString(xxx,radix)
System.out.println(i.toString()); //Prints 15
System.out.println(Float.toString(10.59f));//Prints 10.59
// Watch out, only Long and Integer have a toString(xxx,radix) method.
System.out.println(Long.toString(15, 16)); //Prints f
// Again only for Long and Integer
// toXxxString()
String s1 = Integer.toBinaryString(1);
String s2 = Integer.toHexString(8);
String s3 = Integer.toHexString(15);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
view raw gistfile1.java hosted with ❤ by GitHub

Finally the last thing that you have to pay attention is, valueOf and parseXxx throw a NumberFormatException if the format of a string is not valid.

No comments: