This document discusses autoboxing and unboxing in Java. It explains that autoboxing allows primitive data types to be automatically wrapped into their corresponding wrapper classes when used where an object is expected. Unboxing is the reverse process where the primitive value is automatically extracted from the wrapper class. This simplifies coding by eliminating the need to manually wrap and unwrap values. Autoboxing and unboxing was introduced in Java 5 to make programming with primitive types and wrapper classes more convenient.
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
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!