Library Classes
Library Classes
Wrapper Classes are library classes in Java which are used to convert a primitive data type into an
object and vice versa
They are present in the ‘java.lang’ package which is a preloaded package in every Java class
java.lang = contains classes for basic programming like – wrapper classes, String, Math functions,etc.
* wrapper classes are needed to convert primitive data types into objects and objects are needed to
pass arguments as references to methods, whenever required
* wrapper classes provide various methods that help in converting various data types to the required
data types
Autoboxing –
Automatic conversion of primitive data types to objects of their corresponding wrapper classes is
known as autoboxing
e.g. –
int a = 12 ;
Integer b = a ; //autoboxing
int a = 12 ;
b = a ; //autoboxing
Unboxing –
Automatic conversion of an object of a wrapper class to its corresponding primitive data type
e.g. –
Integer a = 23 ;
int b = a ; //unboxing
Integer a = 23 ;
int b = a.intValue() ;// unboxing ?? *doubt* probably not unboxing but manual boxing
Parsing – refers to analysing a String and extracting relevant data from it as per the requirement
i. String to integer –
Integer.parseInt()
Integer.valueOf()
String s = “25” ;
int i = Integer.parseInt(s) ;
or
int i = Integer.valueOf(s) ;
Long.parseLong()
Long.valueOf()
String s = “123456789” ;
long l = Long.parseLong(s) ;
or
long l = Long.valueOf(s) ;
Float.parseFloat()
Float.valueOf()
String s = “12.45” ;
float f = Float.parseFloat(s) ;
or
float f = Float.valueOf(s) ;
Double.parseDouble()
Double.valueOf()
String s = “14.123” ;
double d = Double.parseDouble(s) ;
or
double d = Double.valueOf(s) ;
Character Functions –
vii. Character.toLowerCase()
viii. Character.toUpperCase()