0% found this document useful (0 votes)
3 views

Chapter 5-Library class

Uploaded by

jitendra.bhu82
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 5-Library class

Uploaded by

jitendra.bhu82
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 5- Library classes in Java

Java class library is a collection of predefined classes providing


methods that can be called at runtime, depending on the need and
usage. These classes are written by the java developers and are
grouped in different packages.
Java Packages
Java packages are a collection of similar library classes or sub
packages.
Wrapper classes
Wrapper classes are used to convert primitive data types into an
object. This process is also known as boxing. Wrapper classes are
defined in java.lang package.

Need of Wrapper Classes

1. They convert primitive data types into objects. Objects


are needed if we wish to modify the arguments passed
into a method (because primitive types are passed by
value).
2. The classes in java.util package handles only objects and
hence wrapper classes help in this case also.
3. Data structures in the Collection framework, such
as ArrayList and Vector, store only objects (reference
types) and not primitive types.
4. An object is needed to support synchronization in
multithreading.
Primitive data types and their corresponding Wrapper
Classes.

Java primitive data type Wrapper class


int Integer
double Double
boolean Boolean
byte Byte
char Character
float Float
long Long
short Short

//example of wrapper class.


class A
{
public static void main(String args[])
{
int k=200;
Integer obj=new Integer(k);
System.out.println(obj);
}
}
Autoboxing and Unboxing

Autoboxing: Automatic conversion of primitive types to the object


of their corresponding wrapper classes is known as autoboxing. For
example – conversion of int to Integer, long to Long, double to
Double etc.
That is when we assign a primitive type value to an object of its
wrapper class directly instead of passing the value to constructor
with “new” keyword.
For example: -
//java program to demonstrate Autoboxing.
class Autoboxing
{
public static void main(String args[])
{
Integer auto=500;
Character ch=’a’;
System.out.println(“value of auto is”+auto);
System.out.println(“value of ch is:-“+ch);
}
}

Unboxing: It is just the reverse process of autoboxing.


Automatically converting an object of a wrapper class to its
corresponding primitive type is known as unboxing. For example –
conversion of Integer to int, Long to long, Double to double, etc.
For example: -
//java program to demonstrate Unboxing.
class Unboxing
{
public static void main(String args[])
{
Character ch=’a’;
char c=ch;
System.out.println(c);
Integer auto=new Integer(300);
int k=auto;
System.out.println(k);
}
}

Methods of Wrapper classes: -


1. Methods of Integer Wrapper class: -

Function Definition
a.Integer.parseInt(string val ) Used to convert string value into
an integer primitive type.
b. Integer.valueOf( ) Same as parseInt() but it returns
integer object.
c. Integer.toString( ) Converts integer to string.

2. Methods of Double Wrapper class: -

Function Definition
a. Double.parseDouble(string val Used to convert string value into
) double primitive type.
b. Double.valueOf( ) Same as parseDouble() but it
returns double object.
c. Double.toString( ) Converts double to string.

3. Methods of Float Wrapper class: -

Function Definition
a. Float.parseFloat(string val ) Used to convert string value into
float primitive type.
b. Float.valueOf( ) Same as parseFloat() but it returns
float object.
c. Float.toString( ) Converts float to string.

4. Methods of Character Wrapper class: -

Function Definition
a. Character.isLetter(char c) It returns true if the
character is letter
otherwise false.
b. Character.isDigit(char c) It returns true if the
character is digit otherwise
false.
c. Character.isLetterOrDigit(char c) It returns true in both the
cases if given character is
letter or digit.
d. Character.isLowerCase(char c) It returns true if the given
character is a lowercase
letter otherwise false.
e. Character.isUpperCase(char c) It returns true if the given
character is a uppercase
letter otherwise false.
f. Character.isWhitespace(char c) It returns true if the given
character is a whitespace
otherwise false.
g. Character.toLowerCase(char c) It converts given character
into lowercase.
h. Character.toUpperCase(char c) It converts given character
into uppercase.

You might also like