You are on page 1of 21

Week 5

Java has a wrapper class for each of the


eight primitive data types:

Primitive Wrapper
Type
Class

Primitive
Type

Wrapper
Class

boolean

Boolean

float

Float

byte
char
double

Byte
Character
Double

int
long
short

Integer
Long
Short

Javas primitive data types (boolean, int,


etc.) are not classes.
Wrapper classes are used in situations
where objects are required, such as for
elements of a Collection (will study later in
Java):

List<Integer> a = new ArrayList<Integer>();


methodRequiringListOfIntegers(a);

Wrapper.valueOf() takes a value (or


string) and returns an object of that
class:

Integer i2 = Integer.valueOf(42);
Boolean b2 = Boolean .valueOf(true);
Long n1 = Long.valueOf(42000000L);

public class WrapperDemo {

public static void main(String args[]) {

Boolean bool = Boolean.valueOf("true");


Character c = new Character('c');
Byte b = Byte.valueOf("12");
Short s = Short.valueOf("2");
Integer i = Integer.valueOf("13245");
Long l = Long.valueOf("12341234");
Float f = Float.valueOf("11234.1234");
Double d =
Double.valueOf("43213241234.12341234123
4");

System.out.println(bool);
System.out.println(c);
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
}

Each wrapper class Type has a method typeValue


to obtain the objects value:

Integer i1 = Integer.valueOf(42;
Boolean b1 = Boolean.valueOf(false);
System.out.println(i1.intValue());
System.out.println(b1.boolValue());
=>
42
false

The Wrapper class for each primitive type has a


method parseType() to parse a string
representation & return the literal value.

Integer.parseInt(42)
=> 42
Boolean.parseBoolean(true) => true
Double.parseDouble(2.71)
=> 2.71
//

Common use: Parsing the arguments to a


program:

public static void main(String[] args) {


// TODO Auto-generated method stub
for (int i = 0; i < args.length; i++) {
try {

System.out.println(Integer.parseInt(args[i]));

} catch (Exception e) {
try {

System.out.println(Float.parseFloat(args[i]));

}
finally {
}
}
}

=>

arg
arg
arg
arg
arg
arg

#
#
#
#
#
#

0
1
2
3
4
5

=
=
=
=
=
=

0
42
999
0.0
1.42
9.0008

Object boolObj = new Boolean(Boolean.TRUE);


Object charObj = new Character('a');
Number byteObj = new Byte("100");
Number shortObj = new Short("32000");
Number intObj = new Integer(2000000);
Number longObj = new Long(500000000000000000L);
Number floatObj = new Float(1.42);
Number doubleObj = new Double(1.42);
System.out.println(boolObj); // true
System.out.println(charObj); // a
..

=>
For Boolean & Character Wrappers (Objects wrappers):
Boolean:true
Character:a
For Number wrappers:
Byte:100
Short:32000
Integer:2000000
Long:500000000000000000
Float:1.42
Double:1.42

byteObj = new Byte(Byte.MAX_VALUE);


shortObj = new Short(Short.MAX_VALUE);
intObj = new Integer(Integer.MAX_VALUE);
longObj = new Long(Long.MAX_VALUE);
floatObj = new Float(Float.MAX_VALUE);
doubleObj = new Double(Double.MAX_VALUE);
printNumValues("MAXIMUM NUMBER VALUES:");

=>
Byte:127
Short:32767
Integer:2147483647
Long:9223372036854775807
Float:3.4028235E38
Double:1.7976931348623157E308

static
static
static
static
static
static
static
static
static
static

int
int
int
int
int
int
int
String
String
String
String

hashCode()
numberOfLeadingZeros(int i)
numberOfTrailingZeros(int i)
reverse(int i)
reverseBytes(int i)
rotateLeft(int i, int distance)
rotateRight(int i, int distance)
toBinaryString(int i)
toHexString(int i)
toOctalString(int i)
toString(int i, int radix)

Constants POSITIVE_INFINITY & NEGATIVE_INFINITY


Constant NaN = Not-a-Number (NaN) value.

Methods isNaN(), isInfinite()

import java.util.Map;
import java.util.TreeMap;
public class Freq {
private static final Integer ONE = new Integer(1);
public static void main(String args[]) {
// Maps word (String) to frequency (Integer)
Map m = new TreeMap();
for (int i=0; i<args.length; i++) {
Integer freq = (Integer) m.get(args[i]);
m.put(args[i], (freq==null ? ONE :
new Integer(freq.intValue() + 1)));
}
System.out.println(m);

This program generates a frequency table of words


on the command line. It uses a Map whose keys are
the words and whose values are the number of
times that each word occurs on the line.
The inner-loop code is a bit convoluted. We continue
by writing the same program with auto boxing,
generics, and an enhanced for loop:

Generic implementation rewritten with auto boxing,


generics, and an enhanced for loop:

public class Freq {


public static void main(String args[]) {
Map<String, Integer> m = new TreeMap<String, Integer>();
for (String word : args) {
Integer freq = m.get(word);
m.put(word, (freq == null ? 1 : freq + 1));
}
System.out.println(m);
}
}

This version is much clearer, and is a good example


of the use of a Wrapper class without the pitfalls of
convoluted primitive-wrapper conversions.

Javas wrapper classes are useful and provide a great deal of


functionality, well beyond that of the primitive types.

Follow Wrapper Classes Study on-line to do some exercises

You might also like