Autoboxing And Unboxing In Java

2,243 views 41 slides Jun 27, 2015
Slide 1
Slide 1 of 41
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41

About This Presentation

This slide describe what is autoboxing and unboxing in Java and give very good basic examples.


Slide Content

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) Now “Auto” word comes into the picture!

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!