Library Classes - Wrapper Class
Library Classes - Wrapper Class
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
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:
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:
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);
Presentation By:Namrata B