Chaper 6 Library Classes
Chaper 6 Library Classes
Library Classes
Library Classes
As you know , java totally depends on
classes and objects. Did you know that java
also provides various predefined classes to
do the task quickly?
Yes, it true.
You don't need to write a code for several
interface.
Wrapper classes
Wrapper classes are the in-bulit classes that
contain primitive data types. These classes are
used to convert primitive data types into objects
and vice-versa with the hep of their
methods.Evry wrapper class ha various methods
too thuse,providing a way to use the primitive
data types (short, double, boolean, etc)as
objects. Every primitive data type is attached to
its wrapper class.
For example float data type is attached with Float
data type.
Need for using a wrapper class
To allow primitive data types to convert
form one data type to another.
To store primitive data types as objects.
Methods to convert Primitive Data types to string.
Integer.toString(int): The toString () method of the
Integer class is used to convert an integer value
into string. It takes an integer value as parameter
and returns after converting it into string. Syntax
of the toString() method is :
String variable = Integer. toString (int);
For example:
int a = 12;
String s = Integer.toString (a);
System.out.println(“Result: “ s);
Output:
Result: 12
(Note: After converting an integer value into
string value, no calculation can be done on
Long.toString(long)
Long.toString(long): The toString () method of
the Long class is used to convert long value into
string. It takes long value as parameter and
returns after converting it into string. Syntax of
the toString() method is :
String variable = Long. toString (long);
For example:
long ln= 9418786910;
String s1 = Long.toString (ln);
System.out.println(“Result: “ s1);
Output:
Result: 9418786910
Double.toString(long)
Double.toString(double): The toString () method
of the Double class is used to convert double
value into string. It takes double value as
parameter and returns after converting it into
string. Syntax of the toString() method is :
String variable = Double. toString (double);
For example:
double dn= 34.5;
String s1 = Double.toString (dn);
System.out.println(“Result: “ s1);
Output:
Result: 34.5
Same for the Float
String variable = Float. toString (float);
Methods to convert String to
integer data types
int variable =Integer.parseInt (String);
Example:
String s = “120”
Int n;
N = Integer.parseInt (s);
System.out.println (Result: “ +n);
Output:
Result : 120
Methods to convert String to long data types
Output:
Result : 12025878
Different Methods of character
Wrapper Class
The Character class wraps a value of the
primitive type char in an object. When a single
digit, letter or any symbols is enclosed single
quotes is known as character data type. For
example , ‘a’ , ‘3’ , ‘/’ etc.
Using the scanner class , you can take the
For example:
boolean b = Charcter.isLetter(‘A’);
Output: true // as ‘A’ is a Charcter
boolean b = Charcter.isLetter(‘1’);
Output: false // as ‘1’ is not a charcter
Methods of Character Class
Character.isDigit(): This method is used to
check weather the parameter passed is a
digit or not. It return a boolean value.
Syntax of the isLetter() is:
boolean variable = Charcter.isDigit(char);
For example:
boolean b = Character.isLetter(‘7’);
Output: true // as ‘7’ is a digit
boolean b = Character.isLetter(‘A’);
Output: false // as ‘A’ is not a digit
Methods of Character Class
Character.isLetterORDigit(): This method is
used to check weather the parameter passed
is a charcter/digit or not. It return a boolean
value.
Syntax of the isLetter() is:
boolean variable = Charcter.isLetter
OrDigit(char);
For example:
boolean b = Charcter.isLetterOrDigit(‘A’);
Output: true // as ‘A’ is a charcter
boolean b = Charcter.isLetter(‘8’);
Output: true // as ‘A’ is a character
boolean b = Charcter.isLetter(‘:’);
Methods of Character Class
Character.isWhitespace(): This method is
used to check weather the parameter
passed is a space or not. It return a
boolean value.
Syntax of the isLetter() is:
boolean variable =
Charcter.isWhitespace(char);
For example:
boolean b = Charcter.isWhitespace(‘ ’);
Output: true // as ‘ ’ is a Whitespace
boolean b = Charcter.isLetter(‘A’);
Output: false // as ‘A’ is not a Whitespace
Methods of Character Class
Character.isUppercase(): This method is
used to check weather the parameter
passed is an uppercase charcter or not. It
return a boolean value.
Syntax of the isLetter() is:
boolean variable =
Charcter.isUppercase(char);
For example:
boolean b = Character.isUpperCase(‘ A’);
Output: true // as ‘ A ’ is a Uppercase
boolean b = Character.isUpperCase(‘a’);
Output: false // as ‘a’ is not a Uppercase
Same for the Lowercase
Methods of Character Class
Character.toUppercase(): This method is
used to convert the parameter passed to
an uppercase letter . It returns a boolean
value.
Syntax of the isLetter() is:
char variable =
Character,toUppercase(char);
For example:
char b = Character.toUpperCase(‘ A’);
Output: A
char b = Character.toUpperCase(‘ a’);
Output: A
Same for the Lowercase
Autoboxing and Unboxing
Autoboxing : Autoboxing conversion from
primitive datatype to its corresponding
object class is called autoboxing. In other
words, if a primitive data type value is
automatically converted into its wrappers
class type, then it is called autoboxing.
For example:
Float F = 25.26;
Unboxing: Automatic conversion from