by Dinesh Thakur

It is better to have all numerical, strings and characters in terms of objects so to avail the facility of methods supplied with those objects. In that case, even if we want to perform any arithmetical operation, we do it with the help of methods instead of using arithmetical operators. But by this approach, performance decreases because method calls are relatively expensive.

So, Java uses both approaches:

We can work with the primitive data types directly by applying arithmetical operators :

                                               int a = a * 10

Also, if we desire, to use certain methods available with the objects of the data types, we use instances of a special set of classes called type wrappers. Each of the primitive data types has a type wrapper class as shown:

Type wrapper classes

Class                           Description

 

Boolean                       Object wrapper for the boolean data type

Character                   Object wrapper for the char data type

Double                        Object wrapper for double data type

Float                           Wrapper for float data type

Integer                       Wraps integer data type

Long                            Type wrapper for long data type

Number                     A superc1ass that defines methods of numeric type wrappers

 

We can create wrappers in a variety of ways depending on the data type. We can create instances of the Integer class:

 

                         Integer a = new Integer(10);

                         Integer b = new Integer(“10”);

 

Now, we can apply a range of methods available in that type wrapper. For example to convert it into another data type (from Integer to double):

 

                       double c = a.doubleValue();

 

We can also employ class methods to perform operations on primitive data types without creating an instance of a class. Following code converts a string to an integer:

 

                       int i = Integer.parseInt(“10");

 

Also, the type wrappers provide public variables that give information about upper and lower bounds of a data type. To get the maximum value a double primitive type can have, we may call the following:

 

System.out.print1n(“Max double value:” + Double.MAX_VALUE);

 

The MAX_VALUE public variable is a class variable of the Double class and is declared as static as in class methods.