The wrapper classes in Java are used to convert primitive data types like int and float into objects. There are eight wrapper classes that correspond to the eight primitive types. Wrapper classes allow primitive types to be used in contexts that require objects, like collections. They provide methods to convert between primitive types and their corresponding wrapper class objects.
Introduction to Java Wrapper classes handled by Dr. T. Abirami, which are used for object types of primitive values.
Wrapper classes convert primitive types to objects. They are necessary for collections and synchronization in multithreading.
Wrapper classes provide two main functions: object creation from primitive types and parsing strings to data types. Features include methods like valueOf() and datatypeValue().
Wrapper classes enable autoboxing (primitive to object) and unboxing (object to primitive), listing eight wrapper classes corresponding to primitive types.
Demonstration of creating wrapper objects using Java, with sample code and usage of valueOf() & parsing methods. Detailed descriptions of wrapper class methods like parseInt(), toString(), and methods to convert between wrapper types and primitives.
Code examples illustrating autoboxing and unboxing, showing conversion between primitives and their respective wrapper classes.
Illustration of the toString() method in a wrapper class to convert Integer to String in Java.
Java Wrapper Classes
HandledBy
Dr.T.Abirami
Associate Professor
Department of IT
Kongu Engineering College Perundurai
2.
Wrapper classes
• Thewrapper classes in Java are used to
convert primitive types (int, char, float, etc)
into corresponding objects.
3.
Need for WrapperClasses
• Generic classes only work with objects and
don't support primitives.
• Data structures in the Collection framework such
as ArrayList and Vector store only the objects
(reference types) and not the primitive types.
• The object is needed to
support synchronization in multithreading.
4.
Importance of Wrapperclasses
There are mainly two uses with wrapper classes.
1) To convert simple data types into objects,
that is, to give object form to a data type; here
constructors are used.
2) To convert strings into data types (known as
parsing operations), here methods of type
parseYYY () are used. YYY->datatypes
5.
Features of theJava wrapper Classes.
1) Wrapper classes convert numeric strings into
numeric values.
2) The way to store primitive data in an object.
3) The valueOf() method is available in all
wrapper classes except Character
4) All wrapper classes have datatypeValue()
method. This method returns the value of the
object as its primitive type.
6.
Wrapper Classes
• Toconvert primitive data type into object and
object into primitive.
• The automatic conversion of primitive into an
object is known as autoboxing
• objects into primitives automatically is called
unboxing
7.
• The eightclasses of the java.lang package are
known as wrapper classes in Java.
• The list of eight wrapper classes are given
below:
Wrapper Classes
Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
• valueOf() method: To create Wrapper object
for given primitive data type or String.
• parseInt() ,parseFloat(), parseDouble()
method : to convert String class (Wrapper
class) to primitive
methods of Wrapper classes
10.
methods of Wrapperclasses
Method Purpose
parseInt(s) returns a signed decimal integer value equivalent to string s
toString(i) returns a new String object representing the integer i
byteValue() returns the value of this Integer as a byte
doubleValue() returns the value of this Integer as a double
floatValue() returns the value of this Integer as a float
intValue() returns the value of this Integer as an int
shortValue() returns the value of this Integer as a short
longValue() returns the value of this Integer as a long
Convert Wrapper classinto Primitive
data types
public class MyClass {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt.intValue());
System.out.println(myDouble.doubleValue());
System.out.println(myChar.charValue());
}
}
13.
autoboxing
public class AutoBoxingTest
{
publicstatic void main(String args[])
{
int num = 10; // int primitive
Integer obj = Integer.valueOf(num);
// creating a wrapper class object
System.out.println(num + " " + obj);
}
}
14.
Unboxing
public class UnboxingTest
{
publicstatic void main(String args[]) {
Integer obj = new Integer(10);
// Creating Wrapper class object
int num = obj.intValue();
// Converting the wrapper object to primitive datatype
System.out.println(num + " " + obj);
}
}
15.
Primitive data typeto Wrapper class
//Autoboxing example of int to Integer
class WrapperExample1{
public static void main(String args[]){
//Converting int into Integer
int a=20;
Integer i=Integer.valueOf(a);
//converting int into Integer explicitly
Integer j=a;
//autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}}
16.
Wrapper class toPrimitive data type
//Unboxing example of Integer to int
class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();
//converting Integer to int explicitly
System.out.println(a+" "+i);
}}
17.
toString()
// Java programto illustrate toString()
class GFG {
public static void main(String[] args)
{
Integer I = new Integer(10);
String s = I.toString();
System.out.println(s);
}
}