by Dinesh Thakur Category: array

Elements that can be added to the Vector must be of type java .lang. Object. In other words, vectors cannot handle primitive data typeslike int, float, char and double as they are not objects. So in order to work with primitive type variables as if they were objects, Java provides class for each of the primitive types. These classes are known as wrapper classes.

A wrapper class wraps the value of a primitive type such as int or double into an object of that class. The Wrapper classes are part of java.lang package which is imported by default into all Java programs. These wrapper classes define an instance variable of that primitive data type and also provides useful constants and methods for conversion between the primitive data types and the objects.  The following table lists the wrapper classes for each of the eight primitive types.

Primitive types Wrapper class 
int Integer
short Short
long Long
byte Byte
float Float
double Double
char Character
boolean Boolean

From the table, we notice that all of the wrapper classes except Integer and Character are named the same as their corresponding primitive type where the first letter of the name is capitalized.

There are several ways to create an instance of primitive variable wrapper class. The first way is to use one of the following two constructors provided by each Wrapper class.
• A constructor that takes a value of primitive type and creates an object of the corresponding wrapper class. For example: To convert a primitive int variable into an Integer wrapper object, we can instantiate the Integer object using Integer constructor as follows,

int a = 35;

Integer aObject = new Integer(a);
The first statement declares an int variable named a and initializes it with a value 35. The second statement, instantiates an Integer object and initializes it with a value stored in variable
a (i.e. 35) and then reference to the object is assigned to the variable aObject.

• A constructor that converts a single string parameter into the object's initial value (except Character,which has no such constructor). For example,
Double dObj = new Double("30.24");
will create a Double wrapper class object dObj which is initialized to a double value 30.24 which is passed as a String argument.
Another way to create a primitive variable wrapper class object is to use one of the valueOf () static methods defined in each class. As in case of constructors, these methods can also take either a primitive value or a string representation of a primitive value as an argument. For example, the statement ,
Integer obj = Integer.valueOf("1010",2);
creates an Integer object obj and initialize it with a integer value 26 (i.e decimal equivalent of
binary number 1010).
Similarly the statement,
Double dObject = Double.valueOf("25.87");
creates a Double object dObject and initialize it with a double value 25.87.
Each wrapper class contains useful methods and constant fields. Each one has a MIN_VALUE
and a MAX_VALUE constant, which represents the minimum and maximum value respectively of the corresponding primitive type. Each wrapper class contains the following methods which are as follows
• static <type> parse<Type> (String s) : This method passes the string passed as an argument and returns the corresponding primitive type. Here <type> may be any of primitive types except char. The <type> written along with parse is same as that of <Type> but with first letter uppercase. For example: When used in association with int primitive type, it will look like
static int parseInt(String s)
Now let us consider the following statements,
String s = "1245";
int val = Integer.parseInt(s);
The second statement will assign an int value of 1245 to int type variable val.
Note that character class does not have this method as it does not have string converting constructor. Also, Boolean class has its own interpretation of the format of s.

The complete list of these methods is shown in the table.

Wrapper Class Method Signature Method Argument
Boolean static boolean parseBoolean( ..) String
Character - Not available - -
Byte static byte parseByte( ..) String or String and radix
Short static short parseShort( ..) String or String and radix
Integer static int parseInt( ..) String or String and radix
Long static long parseLong( ..) String or String and radix
Float static float parseFloat( ..) String
Double static double parseDouble (..) double or String

In table, the radix is the numeric base which can be 2,8, 10 or 16 for binary, octal, decimal
and hexadecimal numbers respectively.
• <type> <type>Value ():This method returns the primitive value corresponding to the current wrapper object. Here, <type> may be any of the primitive types except char. For example, Consider the following statements,
Integer obj = new Integer(67);
int i = obj.intValue();
The second statement converts the wrapped Integer to int primitive type. Therefore, i will contain 67.
The Character class does not support this method.
• String toString ():This method returns the string with the value of primitive wrapper object. All the numeric wrapper classes support this method. For example,
Double d = new Double("56.845");
String str = d.toString();
• static <Type> valueOf (String str) : This method creates a new object of the specified wrapper class <Type> whose value is initialized with the value of string passed as an argument. For example,
Long lObject = Long.valueOf("25678972L");
assigns a value 25678972 to the Long object lObject.
In addition to these methods, each wrapper class individually supports many other methods. You can get details from the Internet.The following program shows use of wrapper class methods.

public class IntWrapperClass

{
      public static void main(String[] args)
    {
  
         int a=35;
         Integer aObject=new Integer(a);
         System.out.println("Maximum value of integer is : "+Integer.MAX_VALUE);
         System.out.println("Maximum value of integer is  : "+Integer.MIN_VALUE);
          System.out.println("Integer.parseInt(\"1245\") : "+Integer.parseInt("1245"));
          System.out.println("aObject.intValue() : "+aObject.intValue());
          System.out.println("aObject.toString() : "+aObject.toString());
          Integer Obj=Integer.valueOf("11010",2);
          System.out.println("Obj.inValue() : "+Obj.intValue());
     }
}

Wrapper Class in Java With Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles