Wrapper Classes

You can't call a method on a Java primitive, such as an int or a double. And sometimes that's a problem because you would like to be able to. Sometimes, you need to hold a list of these kinds of values, and you want to use the classes in the Java Collections Framework; the problem here is that none of those data structures, such as ArrayList, Hashtable, or Vector, will hold primitive types. They only hold objects.

So what's a girl to do? Use the wrapper classes that come with the Java API. There is a class in the java.lang package that corresponds to each of the primitive types. Table 11-1 shows these classes.

11-1. The Primitive Wrapper Classes
WRAPPERPRIMITIVE
Booleanboolean
Bytebyte
Shortshort
Characterchar
Integerint
Longlong
Float ...

Get Java Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.