Thursday, 5 April 2012

Wrapper classes


Wrapper classes

Java uses simple types, such as int and char, for performance reasons. These data types are not part of the object hierarchy. They are passed by value to methods and cannot be directly passed by reference. Also, there is no way for two methods to refer to the same instance of an int. At times, there is a need to create an object representation for one of these simple types. For example, there are enumeration classes that deal only with objects; to store a simple type in one of these classes, you need to wrap the simple type in a class. To address this need, Java provides classes that correspond to each of the simple types. In essence, these classes encapsulate, or wrap, the simple types within a class. Thus, they are commonly referred to as type wrappers.

Wrapper classes

Simple type                              Wrapper class
boolean                                                 Boolean
char                                            Character
double                                         Double
float                                            Float
int                                              Integer
long                                            Long

Converting primitive number to object number using constructore methods

Constructor calling

Conversion action
Integer intval = new integer(i);
Primitive integer to integer object
Float floatval = new float(f);
Primitive float to float object
Double doubleval = new double(d);
Primitive double to double object
Long longval = new long(l);
Primitive long to long object

Converting object number to primitive numbers using type value() method

Method calling
Conversion action

Int I = Intval.intvalue();
Object to primitive integer
Float f = Floatval.floatvalue();
Object to primitive float
Long l = Longval.longvalue();
Object to primitive long
Double d = Doubleval.doublevalue();
Object to primitive double

Converting number to strings using to string () method

              Method calling
               Conversion action

Str = integer.toString(i);
Primitive integer to string
Str = float.toString(f);
Primitive float to string
Str = long.toString(l);
Primitive long to string
Str = double.toString(d);
Primitive double to string

Converting string objects to numeric objects using the static method valueof ().

                Method calling
             Conversion action

Doubleval = double.valueof(str);
Conversion string to double object
Floatval = float.valueof(str);
Conversion string to float object
Intval = integer.valueof(str);
Conversion string to integer object
Longval = long.valueof(str);
Conversion string to long object

Converting numeric strings to primitive numbers using parsing methods


              Method calling
             Conversion action

int i = integer.parseInt(str);
Conversion string to primitive integer
long i = long.parseLong(str);
Conversion string to primitive long

parseInt() and parseLong() methods throws a NumberFormatException.

Posted By : Ruchita Pandya

No comments:

Post a Comment