SlideShare a Scribd company logo
1 of 41
Autoboxing and Unboxing in
JAVA
Chathuranga Kasun Bamunusingha
Quick Review of Java
• Java is an oo language
• Can view everything as an object
Quick Review of Java
• Java is an oo language
• Can view everything as an object
• There are two type of variables in java
• Primitive (int,char,bool…..)
• Object
Quick Review of Java
• Java is an oo language
• Can view everything as an object
• There are two type of variables in java
• Primitive (int,char,bool…..)
• Object
• Simple data types can be converted into
objects
• Using “Wrapper” classes
Quick Review of Java
• Wrapper classes wrap primitive data
types and give it an object appearance
• Int -> Integer
• Char -> Character
• Use of wrapper classes
• To convert simple data types into objects
• To convert strings into relavent data types
• Ex
• Integer.parseInt(“100”);
Quick Review of Java
• Wrapper classes wrap primitive data
types and give it an object appearance
• Int -> Integer
• Char -> Character
Quick Review of Java
● Wrapper class provides methods to
wrap and unwrap objects
◦ Ex:
⚫Wrap -> Integer k=new Integer(100);
⚫Unwrap -> int a=k.intValue();
⚫It simply like a “Chocolate”!
⚫Manufacture wrap the chocolate with some paper(wrapper)
⚫User takes it and remove the paper(wrapper) and eat
Autoboxing and Unboxing
● Autoboxing
◦ Wrap a primitive data type into its object
type
Autoboxing and Unboxing
● Autoboxing
◦ Wrap a primitive data type into its object
type
◦ Unboxing
⚫Unwrap(get the primitive value) from its relevant
object
Autoboxing and Unboxing
● Autoboxing
◦ Wrap a primitive data type into its object
type
◦ Unboxing
⚫Unwrap(get the primitive value) from its relevant
object
⚫I made some mistakes!!!!!!
⚫ How the “Auto” parts comes to the picture!
⚫ You just wait ;)
Back To Java History
● I’m going to write some code in java
4.0!
Back To Java History
● Now I want to add “3” into
listOfNumbers arraylist
● listOfNumbers.add(3);
Back To Java History
● Now I want to add “3” into
listOfNumbers arraylist
● listOfNumbers.add(3);
◦ This gives me an ERROR!!!
Back To Java History
● Now I want to add “3” into
listOfNumbers arraylist
● listOfNumbers.add(3);
◦ This gives me an ERROR!!!
◦ There is no add(int) method , only
add(object) method
Back To Java History
● Now I want to add “3” into
listOfNumbers arraylist
● listOfNumbers.add(3);
◦ This gives me an ERROR!!!
◦ There is no add(int) method , only
add(object) method
◦ listOfNumbers.add(new Integer(3));
Back To Java History
● If I need to take that number “3” back,
then
Back To Java History
● If I need to take that number “3” back,
then
● It comes out as type of Object
◦ Integer one = (Integer)
listOfNumbers.get(0);
Back To Java History
● If I need to take that number “3” back,
then
● It comes out as type of Object
◦ Integer one = (Integer)
listOfNumbers.get(0);
◦ Finally you can get the primitive out of the
Integer
• int intOne = one.intValue();
What is the problem mate!
● In every time you (the programmer)
has to do the wrapping(boxing) and
unwrapping (unboxing) manually
What is the problem mate!
● In every time you (the programmer)
has to do the wrapping(boxing) and
unwrapping (unboxing) manually
◦ It makes coding life bit difficult
◦ It can be introduce bugs in the program
What is the problem mate!
● In every time you (the programmer)
has to do the wrapping(boxing) and
unwrapping (unboxing) manually
◦ It makes coding life bit difficult
◦ It can be introduce bugs in the program
◦ How to solve this ?
What is the problem mate!
● In every time you (the programmer)
has to do the wrapping(boxing) and
unwrapping (unboxing) manually
◦ It makes coding life bit difficult
◦ It can be introduce bugs in the program
◦ How to solve this ?
• If compiler do this automatically for the
programmer then life will be easy!
• That exactly java 5 has done!
• We called it “Autoboxing and unboxing”
Back To Java 5
● Let’s do the previous example in java
5
Back To Java 5
● Let’s do the previous example in java
5
● Make an ArrayList of type Integer
◦ ArrayList<Integer> listOfNumbers = new
ArrayList<Integer>();
Back To Java 5
● Let’s do the previous example in java
5
● Make an ArrayList of type Integer
◦ ArrayList<Integer> listOfNumbers = new
ArrayList<Integer>();
◦ Just add it!
Back To Java 5
● Let’s do the previous example in java
5
● Make an ArrayList of type Integer
◦ ArrayList<Integer> listOfNumbers = new
ArrayList<Integer>();
◦ Just add it!
• listOfNumbers.add(3);
• Here converting int 3 into Integer object has
been done by the compiler itself (Autoboxing)
Back To Java 5
● If I need to get the int value from
Integer object ,
Back To Java 5
● If I need to get the int value from
Integer object ,
◦ int num = listOfNumbers.get(0);
◦ Here getting int value from Integer object
is done by the compiler itself
Back To Java 5
● If I need to get the int value from
Integer object ,
◦ int num = listOfNumbers.get(0);
◦ Here getting int value from Integer object
is done by the compiler itself
◦ That is intValue() method is called by the
compiler
◦ We called it “unboxing”!
Lets Have Fun With Autoboxing
and Unboxing!
● As method arguments
Lets Have Fun With Autoboxing
and Unboxing!
● As method arguments
◦ takeNumber method takes an Integer
object as input parameter. But we can use
int also!
Lets Have Fun With Autoboxing
and Unboxing!
● Return values
Lets Have Fun With Autoboxing
and Unboxing!
● Return values
◦ giveNumber method return int. but it can
be used int or Integer type!
Lets Have Fun With Autoboxing
and Unboxing!
● Boolean expressions
Lets Have Fun With Autoboxing
and Unboxing!
● Boolean expressions
Lets Have Fun With Autoboxing
and Unboxing!
● Operations on numbers
◦ Compiler do the trick!
Lets Have Fun With Autoboxing
and Unboxing!
● Assignments
Lets Have Fun With Autoboxing
and Unboxing!
● Assignments
End OF The Journey
● That’s all about autoboxing and
unboxing!
End OF The Journey
● That’s all about autoboxing and
unboxing!
● Any questions ???
End OF The Journey
● That’s all about autoboxing and
unboxing!
● Any questions ???
● Thank you!

More Related Content

What's hot

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 

What's hot (20)

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Exception handling
Exception handlingException handling
Exception handling
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
QSpiders - Jdk Jvm Jre and Jit
QSpiders - Jdk Jvm Jre and JitQSpiders - Jdk Jvm Jre and Jit
QSpiders - Jdk Jvm Jre and Jit
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Java Streams
Java StreamsJava Streams
Java Streams
 
class and objects
class and objectsclass and objects
class and objects
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Packages in java
Packages in javaPackages in java
Packages in java
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 

Similar to Autoboxing And Unboxing In Java

Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
Chikugehlot
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
jeffz
 

Similar to Autoboxing And Unboxing In Java (20)

Java ap is you should know
Java ap is you should knowJava ap is you should know
Java ap is you should know
 
Hotfixing iOS apps with Javascript
Hotfixing iOS apps with JavascriptHotfixing iOS apps with Javascript
Hotfixing iOS apps with Javascript
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
 
02basics
02basics02basics
02basics
 
Java principles
Java principlesJava principles
Java principles
 
Lagergren jvmls-2013-final
Lagergren jvmls-2013-finalLagergren jvmls-2013-final
Lagergren jvmls-2013-final
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript Everywhere
 
Javascript omg!
Javascript omg!Javascript omg!
Javascript omg!
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 
About Python
About PythonAbout Python
About Python
 
Dynamic Python
Dynamic PythonDynamic Python
Dynamic Python
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09
 
Programming in java basics
Programming in java  basicsProgramming in java  basics
Programming in java basics
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 

Autoboxing And Unboxing In Java

  • 1. Autoboxing and Unboxing in JAVA Chathuranga Kasun Bamunusingha
  • 2. Quick Review of Java • Java is an oo language • Can view everything as an object
  • 3. Quick Review of Java • Java is an oo language • Can view everything as an object • There are two type of variables in java • Primitive (int,char,bool…..) • Object
  • 4. Quick Review of Java • Java is an oo language • Can view everything as an object • There are two type of variables in java • Primitive (int,char,bool…..) • Object • Simple data types can be converted into objects • Using “Wrapper” classes
  • 5. Quick Review of Java • Wrapper classes wrap primitive data types and give it an object appearance • Int -> Integer • Char -> Character • Use of wrapper classes • To convert simple data types into objects • To convert strings into relavent data types • Ex • Integer.parseInt(“100”);
  • 6. Quick Review of Java • Wrapper classes wrap primitive data types and give it an object appearance • Int -> Integer • Char -> Character
  • 7. Quick Review of Java ● Wrapper class provides methods to wrap and unwrap objects ◦ Ex: ⚫Wrap -> Integer k=new Integer(100); ⚫Unwrap -> int a=k.intValue(); ⚫It simply like a “Chocolate”! ⚫Manufacture wrap the chocolate with some paper(wrapper) ⚫User takes it and remove the paper(wrapper) and eat
  • 8. Autoboxing and Unboxing ● Autoboxing ◦ Wrap a primitive data type into its object type
  • 9. Autoboxing and Unboxing ● Autoboxing ◦ Wrap a primitive data type into its object type ◦ Unboxing ⚫Unwrap(get the primitive value) from its relevant object
  • 10. Autoboxing and Unboxing ● Autoboxing ◦ Wrap a primitive data type into its object type ◦ Unboxing ⚫Unwrap(get the primitive value) from its relevant object ⚫I made some mistakes!!!!!! ⚫ How the “Auto” parts comes to the picture! ⚫ You just wait ;)
  • 11. Back To Java History ● I’m going to write some code in java 4.0!
  • 12. Back To Java History ● Now I want to add “3” into listOfNumbers arraylist ● listOfNumbers.add(3);
  • 13. Back To Java History ● Now I want to add “3” into listOfNumbers arraylist ● listOfNumbers.add(3); ◦ This gives me an ERROR!!!
  • 14. Back To Java History ● Now I want to add “3” into listOfNumbers arraylist ● listOfNumbers.add(3); ◦ This gives me an ERROR!!! ◦ There is no add(int) method , only add(object) method
  • 15. Back To Java History ● Now I want to add “3” into listOfNumbers arraylist ● listOfNumbers.add(3); ◦ This gives me an ERROR!!! ◦ There is no add(int) method , only add(object) method ◦ listOfNumbers.add(new Integer(3));
  • 16. Back To Java History ● If I need to take that number “3” back, then
  • 17. Back To Java History ● If I need to take that number “3” back, then ● It comes out as type of Object ◦ Integer one = (Integer) listOfNumbers.get(0);
  • 18. Back To Java History ● If I need to take that number “3” back, then ● It comes out as type of Object ◦ Integer one = (Integer) listOfNumbers.get(0); ◦ Finally you can get the primitive out of the Integer • int intOne = one.intValue();
  • 19. What is the problem mate! ● In every time you (the programmer) has to do the wrapping(boxing) and unwrapping (unboxing) manually
  • 20. What is the problem mate! ● In every time you (the programmer) has to do the wrapping(boxing) and unwrapping (unboxing) manually ◦ It makes coding life bit difficult ◦ It can be introduce bugs in the program
  • 21. What is the problem mate! ● In every time you (the programmer) has to do the wrapping(boxing) and unwrapping (unboxing) manually ◦ It makes coding life bit difficult ◦ It can be introduce bugs in the program ◦ How to solve this ?
  • 22. What is the problem mate! ● In every time you (the programmer) has to do the wrapping(boxing) and unwrapping (unboxing) manually ◦ It makes coding life bit difficult ◦ It can be introduce bugs in the program ◦ How to solve this ? • If compiler do this automatically for the programmer then life will be easy! • That exactly java 5 has done! • We called it “Autoboxing and unboxing”
  • 23. Back To Java 5 ● Let’s do the previous example in java 5
  • 24. Back To Java 5 ● Let’s do the previous example in java 5 ● Make an ArrayList of type Integer ◦ ArrayList<Integer> listOfNumbers = new ArrayList<Integer>();
  • 25. Back To Java 5 ● Let’s do the previous example in java 5 ● Make an ArrayList of type Integer ◦ ArrayList<Integer> listOfNumbers = new ArrayList<Integer>(); ◦ Just add it!
  • 26. Back To Java 5 ● Let’s do the previous example in java 5 ● Make an ArrayList of type Integer ◦ ArrayList<Integer> listOfNumbers = new ArrayList<Integer>(); ◦ Just add it! • listOfNumbers.add(3); • Here converting int 3 into Integer object has been done by the compiler itself (Autoboxing)
  • 27. Back To Java 5 ● If I need to get the int value from Integer object ,
  • 28. Back To Java 5 ● If I need to get the int value from Integer object , ◦ int num = listOfNumbers.get(0); ◦ Here getting int value from Integer object is done by the compiler itself
  • 29. Back To Java 5 ● If I need to get the int value from Integer object , ◦ int num = listOfNumbers.get(0); ◦ Here getting int value from Integer object is done by the compiler itself ◦ That is intValue() method is called by the compiler ◦ We called it “unboxing”!
  • 30. Lets Have Fun With Autoboxing and Unboxing! ● As method arguments
  • 31. Lets Have Fun With Autoboxing and Unboxing! ● As method arguments ◦ takeNumber method takes an Integer object as input parameter. But we can use int also!
  • 32. Lets Have Fun With Autoboxing and Unboxing! ● Return values
  • 33. Lets Have Fun With Autoboxing and Unboxing! ● Return values ◦ giveNumber method return int. but it can be used int or Integer type!
  • 34. Lets Have Fun With Autoboxing and Unboxing! ● Boolean expressions
  • 35. Lets Have Fun With Autoboxing and Unboxing! ● Boolean expressions
  • 36. Lets Have Fun With Autoboxing and Unboxing! ● Operations on numbers ◦ Compiler do the trick!
  • 37. Lets Have Fun With Autoboxing and Unboxing! ● Assignments
  • 38. Lets Have Fun With Autoboxing and Unboxing! ● Assignments
  • 39. End OF The Journey ● That’s all about autoboxing and unboxing!
  • 40. End OF The Journey ● That’s all about autoboxing and unboxing! ● Any questions ???
  • 41. End OF The Journey ● That’s all about autoboxing and unboxing! ● Any questions ??? ● Thank you!