SlideShare a Scribd company logo
5
Most read
9
Most read
12
Most read
Java Wrapper Classes
Handled By
Dr.T.Abirami
Associate Professor
Department of IT
Kongu Engineering College Perundurai
Wrapper classes
• The wrapper classes in Java are used to
convert primitive types (int, char, float, etc)
into corresponding objects.
Need for Wrapper Classes
• 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.
Importance of Wrapper classes
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
Features of the Java 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.
Wrapper Classes
• To convert 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
• The eight classes 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
Creating Wrapper Objects
public class MyClass {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}
• 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
methods of Wrapper classes
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
Creating Wrapper Objects
public class MyClass {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}
Convert Wrapper class into 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());
}
}
autoboxing
public class AutoBoxingTest
{
public static void main(String args[])
{
int num = 10; // int primitive
Integer obj = Integer.valueOf(num);
// creating a wrapper class object
System.out.println(num + " " + obj);
}
}
Unboxing
public class UnboxingTest
{
public static 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);
}
}
Primitive data type to 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);
}}
Wrapper class to Primitive 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);
}}
toString()
// Java program to illustrate toString()
class GFG {
public static void main(String[] args)
{
Integer I = new Integer(10);
String s = I.toString();
System.out.println(s);
}
}

More Related Content

PPS
Wrapper class
PDF
Strings in java
PDF
Computer Science Thesis Defense
PPT
Introduction to html
PDF
Introduction to Java Programming Language
PPT
Uml class-diagram
PDF
Python tuple
PPTX
C# classes objects
Wrapper class
Strings in java
Computer Science Thesis Defense
Introduction to html
Introduction to Java Programming Language
Uml class-diagram
Python tuple
C# classes objects

What's hot (20)

PPT
Abstract class in java
PDF
Arrays in Java
PPTX
6. static keyword
PPTX
Java abstract class & abstract methods
PPTX
Type casting in java
PPTX
Inheritance in java
PDF
Inheritance In Java
PPT
Java static keyword
PPTX
I/O Streams
PPT
Final keyword in java
PDF
Class and Objects in Java
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Method overloading
PPTX
Java constructors
PPTX
Constructor in java
PPTX
PPTX
Methods in java
PPT
9. Input Output in java
PPTX
Super Keyword in Java.pptx
PPT
Java And Multithreading
Abstract class in java
Arrays in Java
6. static keyword
Java abstract class & abstract methods
Type casting in java
Inheritance in java
Inheritance In Java
Java static keyword
I/O Streams
Final keyword in java
Class and Objects in Java
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Method overloading
Java constructors
Constructor in java
Methods in java
9. Input Output in java
Super Keyword in Java.pptx
Java And Multithreading
Ad

Similar to Wrapper classes (20)

PPTX
Java tutorial part 3
PPTX
Java Unit 2(Part 1)
PPTX
DAY_1.3.pptx
PPT
Java Tutorials
PPT
JavaTutorials.ppt
PPTX
Class introduction in java
PPT
java training faridabad
PPTX
L9 wrapper classes
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
PPTX
Class and Object.pptx from nit patna ece department
ODP
Synapseindia reviews.odp.
PPT
PDF
oblect oriented programming language in java notes .pdf
PPT
Introduction to Java(basic understanding).ppt
PPTX
class as the basis.pptx
PPT
Java Fundamentals.pptJava Fundamentals.ppt
PPTX
Module 1.pptx
PPTX
Java-Intro.pptx
Java tutorial part 3
Java Unit 2(Part 1)
DAY_1.3.pptx
Java Tutorials
JavaTutorials.ppt
Class introduction in java
java training faridabad
L9 wrapper classes
Class and Object JAVA PROGRAMMING LANG .pdf
Class and Object.pptx from nit patna ece department
Synapseindia reviews.odp.
oblect oriented programming language in java notes .pdf
Introduction to Java(basic understanding).ppt
class as the basis.pptx
Java Fundamentals.pptJava Fundamentals.ppt
Module 1.pptx
Java-Intro.pptx
Ad

More from Kongu Engineering College, Perundurai, Erode (20)

PPTX
Event Handling -_GET _ POSTimplementation.pptx
PPTX
Introduction to Generative AI refers to a subset of artificial intelligence
PPTX
Introduction to Microsoft Power BI is a business analytics service
PPTX
Connect to NoSQL Database (MongoDB) using Node JS & Connect Node.js with NoSQ...
PPTX
concept of server-side JavaScript / JS Framework: NODEJS
PPTX
Node.js web-based Example :Run a local server in order to start using node.js...
PPT
Concepts of Satellite Communication and types and its applications
PPT
Concepts of Mobile Communication Wireless LANs, Bluetooth , HiperLAN
PPTX
Web Technology Introduction framework.pptx
PPTX
Computer Network - Unicast Routing Distance vector Link state vector
PPT
Android SQLite database oriented application development
PPT
Android Application Development Programming
PPTX
Introduction to Spring & Spring BootFramework
PPTX
A REST API (also called a RESTful API or RESTful web API) is an application p...
PPTX
SOA and Monolith Architecture - Micro Services.pptx
PPTX
Connect to NoSQL Database using Node JS.pptx
PPTX
Event Handling -_GET _ POSTimplementation.pptx
Introduction to Generative AI refers to a subset of artificial intelligence
Introduction to Microsoft Power BI is a business analytics service
Connect to NoSQL Database (MongoDB) using Node JS & Connect Node.js with NoSQ...
concept of server-side JavaScript / JS Framework: NODEJS
Node.js web-based Example :Run a local server in order to start using node.js...
Concepts of Satellite Communication and types and its applications
Concepts of Mobile Communication Wireless LANs, Bluetooth , HiperLAN
Web Technology Introduction framework.pptx
Computer Network - Unicast Routing Distance vector Link state vector
Android SQLite database oriented application development
Android Application Development Programming
Introduction to Spring & Spring BootFramework
A REST API (also called a RESTful API or RESTful web API) is an application p...
SOA and Monolith Architecture - Micro Services.pptx
Connect to NoSQL Database using Node JS.pptx

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
composite construction of structures.pdf
PPTX
web development for engineering and engineering
DOCX
573137875-Attendance-Management-System-original
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Digital Logic Computer Design lecture notes
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Lecture Notes Electrical Wiring System Components
Foundation to blockchain - A guide to Blockchain Tech
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
composite construction of structures.pdf
web development for engineering and engineering
573137875-Attendance-Management-System-original
bas. eng. economics group 4 presentation 1.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
UNIT 4 Total Quality Management .pptx
Digital Logic Computer Design lecture notes

Wrapper classes

  • 1. Java Wrapper Classes Handled By Dr.T.Abirami Associate Professor Department of IT Kongu Engineering College Perundurai
  • 2. Wrapper classes • The wrapper classes in Java are used to convert primitive types (int, char, float, etc) into corresponding objects.
  • 3. Need for Wrapper Classes • 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 Wrapper classes 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 the Java 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 • To convert 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 eight classes 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
  • 8. Creating Wrapper Objects public class MyClass { public static void main(String[] args) { Integer myInt = 5; Double myDouble = 5.99; Character myChar = 'A'; System.out.println(myInt); System.out.println(myDouble); System.out.println(myChar); } }
  • 9. • 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 Wrapper classes 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
  • 11. Creating Wrapper Objects public class MyClass { public static void main(String[] args) { Integer myInt = 5; Double myDouble = 5.99; Character myChar = 'A'; System.out.println(myInt); System.out.println(myDouble); System.out.println(myChar); } }
  • 12. Convert Wrapper class into 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 { public static 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 { public static 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 type to 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 to Primitive 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 program to illustrate toString() class GFG { public static void main(String[] args) { Integer I = new Integer(10); String s = I.toString(); System.out.println(s); } }