LIBRARY CLASSES
WRAPPER CLASSES , AUTO BOXING AND UNBOXING
Presentation By:Namrata B
Let’s Revise
Types of Data Types
char
byte
Class
Composite / short
Non- Primitive data
String Primitive data Datatype type int
type
long
Arrays
float
double
Presentation By:Namrata B
boolean
CLASS IS KNOWN AS COMPOSITE DATA TYPE
• A composite data type is one which is composed with
various primitive data type.
• eg: A class defined with various primitive data types such
as int, double etc; so it is known as a composite data type;
and is used to create objects which hold similar types of
values and behaviors (functions).
Presentation By:Namrata B
Let’s Revise
Type Conversion / Type Casting
Implicit/ widening Explicit/ narrowing
from lower from higher
primitive data primitive data
to higher to lower
primitive data primitive data
Presentation By:Namrata B
HOW TO CONVERT PRIMITIVE TO OBJECT?
THE WRAPPER CLASS IN JAVA PROVIDES
THE MECHANISM TO CONVERT PRIMITIVE
INTO OBJECT AND OBJECT INTO PRIMITIVE.
Presentation By:Namrata B
The eight classes of the java.lang package are known as wrapper classes in Java.
The list of eight wrapper classes are given below:
Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Presentation By:Namrata B
AUTOBOXING AND UNBOXING
• AUTOBOXING : The automatic conversion of primitive data type into
its corresponding wrapper class is known as autoboxing, for example, byte
to Byte, char to Character, int to Integer, long to Long, float to Float,
boolean to Boolean, double to Double, and short to Short.
Example:
//java program to convert primitive into objects
//autoboxing example of int to Integer
class wrapperexample1{
public static void main(){
int a=20;
Integer j=a;//autoboxing, now compiler will write Integer.valueof(a) implicitly
System.out.println(a+" "+j);
}}
Presentation By:Namrata B
UNBOXING:The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing.
Example:
//Java program to convert object into primitives
//unboxing example of integer to int
class wrapperexample2
{
public static void main()
{
//converting Integer to int
Integer a=new Integer(3);
int j=a;//unboxing
System.out.println(a+" "+j);
}}
Presentation By:Namrata B
Converting a String to Primitive Datatype:
Following Methods of Wrapper class are used to convert String to
Primitive datatype:
int parseInt(String s),
long parseLong(String s),
float parseFloat(String s),
double parseDouble(String s)
These methods of Wrapper class are present in java.lang package.
Presentation By:Namrata B
Eg:
class parse_method
{
public static void main() output:
{ 123456
String str1="123",str2="456"; 579
System.out.println(str1+str2);
int no1=Integer.parseInt(str1);
int no2=Integer.parseInt(str2);
System.out.println(no1+no2);
}
}
Presentation By:Namrata B
Let’s Solve
1. Write a java statement to convert a String st into a float value.
Ans: float no=Float.parseFloat(st);
2.What will be the output of following statement:
double no1= Double.parseDouble(“34”);
Ans: 34.0
3. Write one word answer for the following:
(i) A method that converts a string to a primitive byte data type
Ans:Byte.parseByte();
(ii) The default initial value of a boolean variable data type.
Ans: false
Presentation By:Namrata B
Let’s Solve
4. For the program given below mark the parts ___1___ and __2__ as
autoboxing or unboxing.
class Example
{
public static void main() Ans:
{ 1. Autoboxing
Integer i;
int sum=0; 2. Unboxing
for(i=1;i<=10;i++) //___1___
{
sum=sum+i; //__2__
}
System.out.println(sum);
}
} Presentation By:Namrata B
Thank You
Presentation By:Namrata B