SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
Programming in Java
Lecture 9: Wrapper Classes
By
Ravi Kant Sahu
Asst. Professor
Lovely Professional University, PunjabLovely Professional University, Punjab
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
 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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
 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.
 So, the following statement using x and y as declared above
is not allowed:
int z = x + y; // wrong!
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
 The data field in an Integer object is only accessible using the
methods of the Integer class.
 One such method is intValue() method which returns an int
equal to the value of the object, effectively "unwrapping" the
Integer object:
int z = x + y.intValue(); // OK!
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
What is the need of Wrapper Classes?
 There are three reasons that we might use a Number object rather
than a primitive:
 As an argument of a method that expects an object (often used
when manipulating collections of numbers).
 To use constants defined by the class, such as MIN_VALUE and
MAX_VALUE, that provide the upper and lower bounds of the
data type.
 To use class methods for converting values to and from other
primitive types, for converting to and from strings, and for
converting between number systems (decimal, octal,
hexadecimal, binary).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Numeric Wrapper Classes
 All of the numeric wrapper classes are subclasses of the
abstract class Number .
 Short, Integer, Double and Long implement Comparable
interface.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Features of Numeric Wrapper Classes
 Provides a way to store primitive data in an object.
 All numeric wrapper classes implement typeValue() method.
 This method returns the value of the object as its primitive
type.
 byteValue(), intValue(), floatValue(), doubleValue() etc.
 Example:
int a = 10; System.out.println(a.floatValue());
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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()
…
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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”);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods implemented by subclasses of Number
 Converts the value of the Number object to the
primitive data type returned.
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Numeric Type Wrapper Classes
 All of the numeric type wrappers inherit the abstract class Number.
 Number declares methods that return the value of an object in each of the
different number formats. These methods are shown here:
byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )
 For example, doubleValue( ) returns the value of an object as a double,
floatValue( ) returns the value as a float, and so on.
 These methods are implemented by each of the numeric type wrappers.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Integer Class
Constructors:
 Integer(i) : constructs an Integer object equivalent to the
integer i
 Integer(s) : constructs an Integer object equivalent to the
string s
Class Methods:
 parseInt(s) : returns a signed decimal integer value equivalent
to string s
 toString(i) : returns a new String object representing the
integer i
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Instance Methods of Integer Class
 byteValue() : returns the value of this Integer as a byte
 doubleValue() : returns the value of this Integer as an double
 floatValue() : returns the value of this Integer as a float
 intValue() : returns the value of this Integer as an int
 longValue() : returns the value of this Integer as a long
 shortValue() : returns the value of this Integer as a short
 toString() : returns a String object representing the value
of this Integer
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
 To obtain a boolean value from a Boolean object, use
booleanValue( ), shown here:
boolean booleanValue( )
 It returns the boolean equivalent of the invoking
object.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Contenu connexe

Tendances (20)

Data types
Data typesData types
Data types
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Java input
Java inputJava input
Java input
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
14 file handling
14 file handling14 file handling
14 file handling
 
Scanner class
Scanner classScanner class
Scanner class
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
Java package
Java packageJava package
Java package
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java swing
Java swingJava swing
Java swing
 

En vedette

En vedette (20)

Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
wrapper classes
wrapper classeswrapper classes
wrapper classes
 
Dependencies, dependencies, dependencies
Dependencies, dependencies, dependenciesDependencies, dependencies, dependencies
Dependencies, dependencies, dependencies
 
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)
 
Array
ArrayArray
Array
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
JavaYDL15
JavaYDL15JavaYDL15
JavaYDL15
 
Itt1 sd uml and oo
Itt1 sd uml and ooItt1 sd uml and oo
Itt1 sd uml and oo
 
OO & UML
OO & UMLOO & UML
OO & UML
 
Basic IO
Basic IOBasic IO
Basic IO
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Exception handling
Exception handlingException handling
Exception handling
 
Java String
Java String Java String
Java String
 
Strings
StringsStrings
Strings
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Event handling
Event handlingEvent handling
Event handling
 
Packages
PackagesPackages
Packages
 
Multi threading
Multi threadingMulti threading
Multi threading
 
OO Development 3 - Models And UML
OO Development 3 - Models And UMLOO Development 3 - Models And UML
OO Development 3 - Models And UML
 

Similaire à Wrapper classes

String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi Kant Sahu
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
Eo gaddis java_chapter_06_Classes and Objects
Eo gaddis java_chapter_06_Classes and ObjectsEo gaddis java_chapter_06_Classes and Objects
Eo gaddis java_chapter_06_Classes and ObjectsGina Bullock
 
Eo gaddis java_chapter_06_5e
Eo gaddis java_chapter_06_5eEo gaddis java_chapter_06_5e
Eo gaddis java_chapter_06_5eGina Bullock
 
Eo gaddis java_chapter_06_5e
Eo gaddis java_chapter_06_5eEo gaddis java_chapter_06_5e
Eo gaddis java_chapter_06_5eGina Bullock
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi Kant Sahu
 
What is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdfWhat is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdfanandanand521251
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vectornurkhaledah
 
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5eEo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5eGina Bullock
 
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5eEo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5eGina Bullock
 

Similaire à Wrapper classes (20)

Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Eo gaddis java_chapter_06_Classes and Objects
Eo gaddis java_chapter_06_Classes and ObjectsEo gaddis java_chapter_06_Classes and Objects
Eo gaddis java_chapter_06_Classes and Objects
 
Eo gaddis java_chapter_06_5e
Eo gaddis java_chapter_06_5eEo gaddis java_chapter_06_5e
Eo gaddis java_chapter_06_5e
 
Eo gaddis java_chapter_06_5e
Eo gaddis java_chapter_06_5eEo gaddis java_chapter_06_5e
Eo gaddis java_chapter_06_5e
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Inheritance
InheritanceInheritance
Inheritance
 
Java tutorial part 3
Java tutorial part 3Java tutorial part 3
Java tutorial part 3
 
DAY_1.3.pptx
DAY_1.3.pptxDAY_1.3.pptx
DAY_1.3.pptx
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
What is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdfWhat is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdf
 
List classes
List classesList classes
List classes
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vector
 
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5eEo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5e
 
Eo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5eEo gaddis java_chapter_08_5e
Eo gaddis java_chapter_08_5e
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 
Hemajava
HemajavaHemajava
Hemajava
 

Plus de Ravi_Kant_Sahu

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaRavi_Kant_Sahu
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java Ravi_Kant_Sahu
 

Plus de Ravi_Kant_Sahu (9)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
Event handling
Event handlingEvent handling
Event handling
 
Jdbc
JdbcJdbc
Jdbc
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Swing api
Swing apiSwing api
Swing api
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 

Dernier

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 

Dernier (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

Wrapper classes

  • 1. Programming in Java Lecture 9: Wrapper Classes By Ravi Kant Sahu Asst. Professor Lovely Professional University, PunjabLovely Professional University, Punjab
  • 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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 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 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 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); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)  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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 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.  So, the following statement using x and y as declared above is not allowed: int z = x + y; // wrong! Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10.  The data field in an Integer object is only accessible using the methods of the Integer class.  One such method is intValue() method which returns an int equal to the value of the object, effectively "unwrapping" the Integer object: int z = x + y.intValue(); // OK! Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. 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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. What is the need of Wrapper Classes?  There are three reasons that we might use a Number object rather than a primitive:  As an argument of a method that expects an object (often used when manipulating collections of numbers).  To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.  To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary). Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. 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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Numeric Wrapper Classes  All of the numeric wrapper classes are subclasses of the abstract class Number .  Short, Integer, Double and Long implement Comparable interface. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. Features of Numeric Wrapper Classes  Provides a way to store primitive data in an object.  All numeric wrapper classes implement typeValue() method.  This method returns the value of the object as its primitive type.  byteValue(), intValue(), floatValue(), doubleValue() etc.  Example: int a = 10; System.out.println(a.floatValue()); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. 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() … Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. 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) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. 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”); Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 19. Methods implemented by subclasses of Number  Converts the value of the Number object to the primitive data type returned. byte byteValue() short shortValue() int intValue() long longValue() float floatValue() double doubleValue() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. 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). Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. 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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 22. Numeric Type Wrapper Classes  All of the numeric type wrappers inherit the abstract class Number.  Number declares methods that return the value of an object in each of the different number formats. These methods are shown here: byte byteValue( ) double doubleValue( ) float floatValue( ) int intValue( ) long longValue( ) short shortValue( )  For example, doubleValue( ) returns the value of an object as a double, floatValue( ) returns the value as a float, and so on.  These methods are implemented by each of the numeric type wrappers. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. Integer Class Constructors:  Integer(i) : constructs an Integer object equivalent to the integer i  Integer(s) : constructs an Integer object equivalent to the string s Class Methods:  parseInt(s) : returns a signed decimal integer value equivalent to string s  toString(i) : returns a new String object representing the integer i Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 24. Instance Methods of Integer Class  byteValue() : returns the value of this Integer as a byte  doubleValue() : returns the value of this Integer as an double  floatValue() : returns the value of this Integer as a float  intValue() : returns the value of this Integer as an int  longValue() : returns the value of this Integer as a long  shortValue() : returns the value of this Integer as a short  toString() : returns a String object representing the value of this Integer Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 25. 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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 26. 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. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 27.  To obtain a boolean value from a Boolean object, use booleanValue( ), shown here: boolean booleanValue( )  It returns the boolean equivalent of the invoking object. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab
  • 28. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)