SlideShare a Scribd company logo
Programming in Java
Lecture 9: Wrapper Classes
Introduction
• Most of the objects collection store objects and not primitive
types.
• Primitive types can be used as object when required.
• As they are objects, they can be stored in any of the collection
and pass this collection as parameters to the methods.
Wrapper Class
• Wrapper classes are classes that allow primitive types to be
accessed as objects.
• Wrapper class is wrapper around a primitive data type because
they "wrap" the primitive data type into an object of that class.
What is Wrapper Class?
• Each of Java's eight primitive data types has a class dedicated to
it.
• They are one per primitive type: Boolean, Byte, Character,
Double, Float, Integer, Long and Short.
• Wrapper classes make the primitive type data to act as objects.
Primitive Data Types and Wrapper Classes
Data Type Wrapper Class
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean
Difference b/w Primitive Data Type and
Object of a Wrapper Class
• The following two statements illustrate the difference between a
primitive data type and an object of a wrapper class:
int x = 25;
Integer y = new Integer(33);
 The first statement declares an int variable named x
and initializes it with the value 25.
• The second statement instantiates an Integer object. The object
is initialized with the value 33 and a reference to the object is
assigned to the object variable y.
• Clearly x and y differ by more than their values:
x is a variable that holds a value;
y is an object variable that holds a reference to an object.
What is the need of Wrapper Classes?
• Wrapper classes are used to be able to use the primitive data-
types as objects.
• Many utility methods are provided by wrapper classes.
To get these advantages we need to use wrapper classes.
Boxing and Unboxing
• The wrapping is done by the compiler.
• if we use a primitive where an object is expected, the compiler boxes the
primitive in its wrapper class.
• Similarly, if we use a number object when a primitive is expected, the compiler
un-boxes the object.
Example of boxing and unboxing:
• Integer x, y; x = 12; y = 15; System.out.println(x+y);
• When x and y are assigned integer values, the compiler boxes the integers
because x and y are integer objects.
• In the println() statement, x and y are unboxed so that they can be added as
integers.
Numeric Wrapper Classes
• All of the numeric wrapper classes are subclasses of the
abstract class Number .
• Short, Integer, Double and Long implement Comparable
interface.
Features of Numeric Wrapper Classes
• All the numeric wrapper classes provide a method to convert a
numeric string into a primitive value.
public static type parseType (String Number)
• parseInt()
• parseFloat()
• parseDouble()
• parseLong()
…
Features of Numeric Wrapper Classes
• All the wrapper classes provide a static method toString to
provide the string representation of the primitive values.
public static String toString (type value)
Example:
public static String toString (int a)
Features of Numeric Wrapper Classes
• All numeric wrapper classes have a static method valueOf,
which is used to create a new object initialized to the value
represented by the specified string.
public static DataType valueOf (String s)
Example:
Integer i = Integer.valueOf (“135”);
Double d = Double.valueOf (“13.5”);
Methods implemented by subclasses of
Number
• Compares this Number object to the argument.
int compareTo(Byte anotherByte)
int compareTo(Double anotherDouble)
int compareTo(Float anotherFloat)
int compareTo(Integer anotherInteger)
int compareTo(Long anotherLong)
int compareTo(Short anotherShort)
• returns int after comparison (-1, 0, 1).
Methods implemented by subclasses of
Number
boolean equals(Object obj)
• Determines whether this number object is equal to the
argument.
• The methods return true if the argument is not null and is an
object of the same type and with the same numeric value.
Character Class
• Character is a wrapper around a char.
• The constructor for Character is :
Character(char ch)
Here, ch specifies the character that will be wrapped by the
Character object being created.
• To obtain the char value contained in a Character object, call
charValue( ), shown here:
char charValue( );
• It returns the encapsulated character.
Boolean Class
• Boolean is a wrapper around boolean values.
• It defines these constructors:
Boolean(boolean boolValue)
Boolean(String boolString)
• In the first version, boolValue must be either true or false.
• In the second version, if boolString contains the string “true”
(in uppercase or lowercase), then the new Boolean object will
be true. Otherwise, it will be false.
• To obtain a boolean value from a Boolean object, use booleanValue(
), shown here:
boolean booleanValue( )
• It returns the boolean equivalent of the invoking object.
L9 wrapper classes

More Related Content

What's hot

String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Java Collections
Java  Collections Java  Collections
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
Ravi_Kant_Sahu
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
Arzath Areeff
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
 
Wrapper classes
Wrapper classes Wrapper classes
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Generics in java
Generics in javaGenerics in java
Generics in java
suraj pandey
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
Amar Jukuntla
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 

What's hot (20)

String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Viewers also liked

Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
Akshay soni
 
Banking structure of india and united kingdom(1)
Banking structure of india and united kingdom(1)Banking structure of india and united kingdom(1)
Banking structure of india and united kingdom(1)
Pooja Yadav
 
How to Get Started with Salesforce Lightning
How to Get Started with Salesforce LightningHow to Get Started with Salesforce Lightning
How to Get Started with Salesforce Lightning
Salesforce Admins
 
Salesforce Lightning Design System
Salesforce Lightning Design SystemSalesforce Lightning Design System
Salesforce Lightning Design System
Durgesh Dhoot
 
Lightning Components Introduction
Lightning Components IntroductionLightning Components Introduction
Lightning Components Introduction
Durgesh Dhoot
 
Introducing the Salesforce Lightning Design System
Introducing the Salesforce Lightning Design SystemIntroducing the Salesforce Lightning Design System
Introducing the Salesforce Lightning Design System
Salesforce Developers
 

Viewers also liked (6)

Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
Banking structure of india and united kingdom(1)
Banking structure of india and united kingdom(1)Banking structure of india and united kingdom(1)
Banking structure of india and united kingdom(1)
 
How to Get Started with Salesforce Lightning
How to Get Started with Salesforce LightningHow to Get Started with Salesforce Lightning
How to Get Started with Salesforce Lightning
 
Salesforce Lightning Design System
Salesforce Lightning Design SystemSalesforce Lightning Design System
Salesforce Lightning Design System
 
Lightning Components Introduction
Lightning Components IntroductionLightning Components Introduction
Lightning Components Introduction
 
Introducing the Salesforce Lightning Design System
Introducing the Salesforce Lightning Design SystemIntroducing the Salesforce Lightning Design System
Introducing the Salesforce Lightning Design System
 

Similar to L9 wrapper classes

Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
Mumbai Academisc
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
KavitaHegde4
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
KavitaHegde4
 
DAY_1.3.pptx
DAY_1.3.pptxDAY_1.3.pptx
DAY_1.3.pptx
ishasharma835109
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
wrapper class.ppt
wrapper class.pptwrapper class.ppt
wrapper class.ppt
Peace80
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
SURBHI SAROHA
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
Syed Afaq Shah MACS CP
 
Java Day-4
Java Day-4Java Day-4
Java Day-4
People Strategists
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in java
Garuda Trainings
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vector
nurkhaledah
 
Quick Scala
Quick ScalaQuick Scala
Quick Scala
Puneet Kumar
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Java
JavaJava
Variable
VariableVariable
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
AshutoshTrivedi30
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
JSpiders Basavanagudi
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
simarsimmygrewal
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Learn ActionScript programming myassignmenthelp.net
Learn ActionScript programming myassignmenthelp.netLearn ActionScript programming myassignmenthelp.net
Learn ActionScript programming myassignmenthelp.net
www.myassignmenthelp.net
 

Similar to L9 wrapper classes (20)

Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
 
DAY_1.3.pptx
DAY_1.3.pptxDAY_1.3.pptx
DAY_1.3.pptx
 
Learning core java
Learning core javaLearning core java
Learning core java
 
wrapper class.ppt
wrapper class.pptwrapper class.ppt
wrapper class.ppt
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 
Java Day-4
Java Day-4Java Day-4
Java Day-4
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in java
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vector
 
Quick Scala
Quick ScalaQuick Scala
Quick Scala
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Java
JavaJava
Java
 
Variable
VariableVariable
Variable
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Learn ActionScript programming myassignmenthelp.net
Learn ActionScript programming myassignmenthelp.netLearn ActionScript programming myassignmenthelp.net
Learn ActionScript programming myassignmenthelp.net
 

More from teach4uin

Controls
ControlsControls
Controls
teach4uin
 
validation
validationvalidation
validation
teach4uin
 
validation
validationvalidation
validation
teach4uin
 
Master pages
Master pagesMaster pages
Master pages
teach4uin
 
.Net framework
.Net framework.Net framework
.Net framework
teach4uin
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
teach4uin
 
Css1
Css1Css1
Css1
teach4uin
 
Code model
Code modelCode model
Code model
teach4uin
 
Asp db
Asp dbAsp db
Asp db
teach4uin
 
State management
State managementState management
State management
teach4uin
 
security configuration
security configurationsecurity configuration
security configuration
teach4uin
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
teach4uin
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
teach4uin
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
teach4uin
 
.Net overview
.Net overview.Net overview
.Net overview
teach4uin
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
teach4uin
 
enums
enumsenums
enums
teach4uin
 
memory
memorymemory
memory
teach4uin
 
array
arrayarray
array
teach4uin
 
storage clas
storage classtorage clas
storage clas
teach4uin
 

More from teach4uin (20)

Controls
ControlsControls
Controls
 
validation
validationvalidation
validation
 
validation
validationvalidation
validation
 
Master pages
Master pagesMaster pages
Master pages
 
.Net framework
.Net framework.Net framework
.Net framework
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Css1
Css1Css1
Css1
 
Code model
Code modelCode model
Code model
 
Asp db
Asp dbAsp db
Asp db
 
State management
State managementState management
State management
 
security configuration
security configurationsecurity configuration
security configuration
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
 
.Net overview
.Net overview.Net overview
.Net overview
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
enums
enumsenums
enums
 
memory
memorymemory
memory
 
array
arrayarray
array
 
storage clas
storage classtorage clas
storage clas
 

Recently uploaded

WPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide DeckWPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide Deck
Lidia A.
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
Zilliz
 
Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...
chetankumar9855
 
find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
huseindihon
 
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptxDublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Kunal Gupta
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
Safe Software
 
The Evolution of Remote Server Management
The Evolution of Remote Server ManagementThe Evolution of Remote Server Management
The Evolution of Remote Server Management
Bert Blevins
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
HackersList
 
How RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptxHow RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptx
SynapseIndia
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
ArgaBisma
 
How to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdfHow to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdf
ChristopherTHyatt
 
Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024
aakash malhotra
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
SynapseIndia
 
ARTIFICIAL INTELLIGENCE (AI) IN MUSIC.pdf
ARTIFICIAL INTELLIGENCE (AI) IN MUSIC.pdfARTIFICIAL INTELLIGENCE (AI) IN MUSIC.pdf
ARTIFICIAL INTELLIGENCE (AI) IN MUSIC.pdf
Inglês no Mundo Digital
 
July Patch Tuesday
July Patch TuesdayJuly Patch Tuesday
July Patch Tuesday
Ivanti
 
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
maigasapphire
 
Comparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdfComparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdf
Andrey Yasko
 
Salesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot WorkshopSalesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot Workshop
CEPTES Software Inc
 
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Bert Blevins
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
Shiv Technolabs
 

Recently uploaded (20)

WPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide DeckWPRiders Company Presentation Slide Deck
WPRiders Company Presentation Slide Deck
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
 
Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...
 
find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
 
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptxDublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
Dublin_mulesoft_meetup_Mulesoft_Salesforce_Integration (1).pptx
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
 
The Evolution of Remote Server Management
The Evolution of Remote Server ManagementThe Evolution of Remote Server Management
The Evolution of Remote Server Management
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
 
How RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptxHow RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptx
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
 
How to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdfHow to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdf
 
Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024Three New Criminal Laws in India 1 July 2024
Three New Criminal Laws in India 1 July 2024
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
 
ARTIFICIAL INTELLIGENCE (AI) IN MUSIC.pdf
ARTIFICIAL INTELLIGENCE (AI) IN MUSIC.pdfARTIFICIAL INTELLIGENCE (AI) IN MUSIC.pdf
ARTIFICIAL INTELLIGENCE (AI) IN MUSIC.pdf
 
July Patch Tuesday
July Patch TuesdayJuly Patch Tuesday
July Patch Tuesday
 
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
 
Comparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdfComparison Table of DiskWarrior Alternatives.pdf
Comparison Table of DiskWarrior Alternatives.pdf
 
Salesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot WorkshopSalesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot Workshop
 
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
Understanding Insider Security Threats: Types, Examples, Effects, and Mitigat...
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
 

L9 wrapper classes

  • 1. Programming in Java Lecture 9: Wrapper Classes
  • 2. Introduction • Most of the objects collection store objects and not primitive types. • Primitive types can be used as object when required. • As they are objects, they can be stored in any of the collection and pass this collection as parameters to the methods.
  • 3. Wrapper Class • Wrapper classes are classes that allow primitive types to be accessed as objects. • Wrapper class is wrapper around a primitive data type because they "wrap" the primitive data type into an object of that class.
  • 4. What is Wrapper Class? • Each of Java's eight primitive data types has a class dedicated to it. • They are one per primitive type: Boolean, Byte, Character, Double, Float, Integer, Long and Short. • Wrapper classes make the primitive type data to act as objects.
  • 5. Primitive Data Types and Wrapper Classes Data Type Wrapper Class byte Byte short Short int Integer long Long char Character float Float double Double boolean Boolean
  • 6. Difference b/w Primitive Data Type and Object of a Wrapper Class • The following two statements illustrate the difference between a primitive data type and an object of a wrapper class: int x = 25; Integer y = new Integer(33);
  • 7.  The first statement declares an int variable named x and initializes it with the value 25.
  • 8. • The second statement instantiates an Integer object. The object is initialized with the value 33 and a reference to the object is assigned to the object variable y.
  • 9. • Clearly x and y differ by more than their values: x is a variable that holds a value; y is an object variable that holds a reference to an object.
  • 10. What is the need of Wrapper Classes? • Wrapper classes are used to be able to use the primitive data- types as objects. • Many utility methods are provided by wrapper classes. To get these advantages we need to use wrapper classes.
  • 11. Boxing and Unboxing • The wrapping is done by the compiler. • if we use a primitive where an object is expected, the compiler boxes the primitive in its wrapper class. • Similarly, if we use a number object when a primitive is expected, the compiler un-boxes the object. Example of boxing and unboxing: • Integer x, y; x = 12; y = 15; System.out.println(x+y); • When x and y are assigned integer values, the compiler boxes the integers because x and y are integer objects. • In the println() statement, x and y are unboxed so that they can be added as integers.
  • 12. Numeric Wrapper Classes • All of the numeric wrapper classes are subclasses of the abstract class Number . • Short, Integer, Double and Long implement Comparable interface.
  • 13. Features of Numeric Wrapper Classes • All the numeric wrapper classes provide a method to convert a numeric string into a primitive value. public static type parseType (String Number) • parseInt() • parseFloat() • parseDouble() • parseLong() …
  • 14. Features of Numeric Wrapper Classes • All the wrapper classes provide a static method toString to provide the string representation of the primitive values. public static String toString (type value) Example: public static String toString (int a)
  • 15. Features of Numeric Wrapper Classes • All numeric wrapper classes have a static method valueOf, which is used to create a new object initialized to the value represented by the specified string. public static DataType valueOf (String s) Example: Integer i = Integer.valueOf (“135”); Double d = Double.valueOf (“13.5”);
  • 16. Methods implemented by subclasses of Number • Compares this Number object to the argument. int compareTo(Byte anotherByte) int compareTo(Double anotherDouble) int compareTo(Float anotherFloat) int compareTo(Integer anotherInteger) int compareTo(Long anotherLong) int compareTo(Short anotherShort) • returns int after comparison (-1, 0, 1).
  • 17. Methods implemented by subclasses of Number boolean equals(Object obj) • Determines whether this number object is equal to the argument. • The methods return true if the argument is not null and is an object of the same type and with the same numeric value.
  • 18. Character Class • Character is a wrapper around a char. • The constructor for Character is : Character(char ch) Here, ch specifies the character that will be wrapped by the Character object being created. • To obtain the char value contained in a Character object, call charValue( ), shown here: char charValue( ); • It returns the encapsulated character.
  • 19. Boolean Class • Boolean is a wrapper around boolean values. • It defines these constructors: Boolean(boolean boolValue) Boolean(String boolString) • In the first version, boolValue must be either true or false. • In the second version, if boolString contains the string “true” (in uppercase or lowercase), then the new Boolean object will be true. Otherwise, it will be false.
  • 20. • To obtain a boolean value from a Boolean object, use booleanValue( ), shown here: boolean booleanValue( ) • It returns the boolean equivalent of the invoking object.