Wrapper classes are those classes that allow primitive data types to be accessed as objects. The wrapper class is one kind of wrapper around a primitive data type.
Wrappers
•
Wrapper classes are those classes that allow primitive data types to be
accessed as objects.
• The
wrapper class is one kind of wrapper around a primitive data type. The wrapper
classes represent the primitive data types in its instance of the class.
• Following
table shows various primitive data types and the corresponding wrapper classes
-
• Methods
to handle wrapper classes are enlisted in the following table -
• Suppose
an object for holding an integer value is created then we can retrieve the
integer value from it using typevalue() method. For instance the object obj
contains an integer value then we can obtain the integer value from obj. It is
as follows -
int
num=obj.intValue();
• Similarly
we can use floatValue(), doubleValue() and longValue().
• Similarly
in order to convert the numerical value to string the toString() method can be
used. For instance
str=Integer.toString(int_val)
• The
variable int_val can be converted to string str.
• For
converting the string to numerical value the parseInt or parseLong methods can
be used.
Points to remember about wrapper classes
1. The
wrapper classes do not contain the constructors.
2. The
methods of the wrapper classes are static.
3. After
assigning the values to the wrapper class we cannot change them.
Example: Java Program
import
java.io.*;
import
java.lang.*;
class
WrapperDemo
{
public
static void main(String[] args)
{
System.out.println("Creating
an object for value 10");
Integer
i=new Integer(10);
System.out.println("Obtaining
the value back from the object: "+i.intValue());
String
str="100";
System.out.println("The
string is: "+str);
System.out.println("Obtaining
the numeric value from the string: "+ Integer.parseInt(str));
}
}
Output
Creating
an object for value 10
Obtaining
the value back from the object: 10
The
string is: 100
Obtaining
the numeric value from the string: 100
Ex. 3.18.1Define wrapper class.
Give the following wrapper class methods with
syntax and use :
1) To convert integer number to
string.
2) To convert numeric string to
integer number.
3) To convert object numbers to
primitive numbers sing type value () method.
Sol. Wrapper class - Refer
section 3.18.
class
WrapperDemo1
{
public
static void main(String args[]).
{
System.out.println("\tInteger
to String Conversion");
int
i=100;
String
str=Integer.toString(i);
System.out.println("int
Value: "+i);
System.out.println("Equivalent
String: "+str);.
System.out.println("\tString
to Integer Conversion");
str="500";
int j=
Integer.parseInt(str);
System.out.println("String:
"+str);
System.out.println("Equivalent
int: "+j);
System.out.println("\tobject
to Primitive number
using
typeValue Conversion");
Integer
a new Integer(1000);//creating object for float value int val a.intValue();
System.out.println("Integer:
"+a);
System.out.println("Equivalent
int value: "+val);
Float
b=new Float(2000);//creating object for float value
float
f=b.floatValue();
System.out.println("Float:
"+b);
System.out.println("Equivalent
float value: "+f);
}
}
Output
Integer
to String Conversion
int
Value: 100
Equivalent
String: 100
String
to Integer Conversion
String:
500
Equivalent
int: 500
object
to Primitive number using typeValue Conversion
Integer:
1000
Equivalent
int value: 1000
Float:
2000.0
Equivalent
float value: 2000.0
Uses of Wrapper Class
1)
Wrapper classes are used to convert numeric strings into numeric values.
2) Wrapper
classes are used to convert numeric value to string using wrapper class.
3)
Wrapper class is a medium to store primitive data type in an object.
4) Using
the typeValue() method we can retrieve value of the object as its primitive
data type.
Review Questions
1.Explain
the use of wrapper classes in Java.
2.
Explain the concept of wrapper classes with illustrative example.
Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : with Example Java Programs | Multithreading - Wrappers
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation