Google+

241. Wrapper Classes







Pre-requisites -


Java uses Primitive data types like int, float, char etc to hold the basic data types supported by the language. 

Example -

int primitive type is used to store integer type data  (i.e. int var = 5 )
char primitive type is used to store characters data type (i.e. char var = 'a' )

Why Java uses Primitive Data Types instead of using Objects for holding the basic data types ?

As using objects for holding the basic data types like integers, characters etc will add overhead to the simplest calculations by causing the performance issues, the Java uses Primitive Data types like int, char etc for holding the basic data types.

Are Primitive Data Types part of the object hierarchy ?

Primitive data types are not part of the object hierarchy and they do not inherit Object Class.

What are Wrapper Classes ?

As we are not able to use Primitive Data Types as objects directly in Java Programming language, we can use Wrapper Class to wrap the Primitive Data Types into objects. Hence Wrapper Classes are used, to wrap the primitive type into an object, so that we can use the Primitive Data Type as an object.

What is the purpose for wrapping the Primitive Data Types into Objects using Wrapper Classes ?

Many times, we need object representation of primitive types. For example, we cannot pass a primitive type by reference (i.e. object) to a method. In order to pass the primitive type as a reference to any method, we have to wrap the primitive type into an object reference using the appropriate Wrapper Class and pass the wrapped object reference to the desired method.

What are the types of Wrapper Classes ?

Each of the 8 primitive data types has a wrapper class dedicated to it as shown in the below table -



 And below is the Wrapper Classes Hierarchy as per the Java API - 


Understanding the above diagram - Object Class is the Parent Class of all the Classes in Java. Number, Character and Boolean Classes are the child classes of Object Class. Byte, Short, Integer, Long, Float, Double are the child classes of the Number Class.

Lets implement the Wrapper Classes one by one in the upcoming posts.







Please comment below to feedback or ask questions.

Integer Wrapper Class will be explained in the next post.





No comments:

Blog Index