Friday, 13 June 2014

Wrapper classes for Primitive types?

In Java programming language, there are eight primitive types and each of these has a corresponding library class of reference type. For example, there is a class java.lang.Integer that corresponds to primitive type int. These kinds of classes are called wrappers. The wrapper classes are immutable, we cannot change a wrapped value after the wrapper has been constructed. They are also final, so we cannot subclass them.

Why do we have wrapper classes for primitive types?
1.The wrapper classes are used whenever a primitive type needs to be treated as an Object. For example, the classes in the Collections API (like ArrayList) can only hold Object references.
2.To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.

No comments:

Post a Comment