Library Classes NOTES

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Sumit Paul Notes

Library Classes
Java Development Kit (JDK) includes all types of software tools which are essentially
needed while writing programs.
The major component of JDK is Java Class Library (JCL) that contains various packages.
Each package of JCL is a collection of classes containing different built-in functions.
There are numerous packages available in JCL. Some important packages which are
frequently used in java programming are:

Java Packages Purpose

Java.lang Contain classes related to string manipulations

Java.io Contain class for input/output operation.

Java.awt Contain classes to implement Graphical User Interface.

Java.util Contain utility classes to implement data structure.

Java.applet Contain classes for implementing applets.

Java.net Contain classes for supporting network operations.

Java.math Contain classes for mathematical operation.

The keyword ‘import’ is used to include a package in a java program.


Syntax: import <package name. *>;
e.g., import java.io.*;
import java.util.*;
Note: The asterisk (*) sign indicates that all the classes of the imported package can be
used in the program.
In case, you want to use only a specific class of package then use the class name in place of
asterisk sign(*).

Primitive data type and Composite data type:


Primitive data: Primitive data types are the fundamental or basic data types which are
created by the system developers. These data types are designed to declare a variable that
can contain a single element (number/character).
For example: int, float, char, double etc.
Composite data: However during programming, Primitive data types are not always
sufficient to handle complex operations. Under such circumstances, a group of similar or
different primitive types may be included to represent a specific data type. It is known as
non-primitive or composite or reference data type.
Sumit Paul Notes
Array is a composite data type that uses similar type of primitive data.
The main difference between the Primitive data type and composite data types are:

Primitive Data Type Composite Data Type

1. It is a fundamental data type. 1. It is a set of Primitive data types.

2. Primitive data types are defined by the 2. Composite data types are defined by the
system developers. users.

Wrapper class
The wrapper class is a class that contains a primitive data type. It is a part of java library
package java.lang.
Whenever an object to a wrapper class is created, a field in memory is allocated to
contain a primitive data.
‘Integer’ is a wrapper class that contains primitive data type ‘int’ and the following
functions:
⚫ Integer.parseInt(): To convert a string into integer data type.
⚫ Integer.toString() : To convert an integer data type into string.

Need of using wrapper classes:


The wrapper classes in java serve two primary purposes:
⚫ To store primitive values in the objects.
⚫ To convert a string data into other primitive types and vice-versa.

There are eight wrapper classes in java. These are:

Wrapper Class Primitive Data Type

Character Char

Byte Byte

Short Short

Integer Int

Long Long

Float Float

Double Double

Boolean boolean
Sumit Paul Notes
Methods of Wrapper Classes:
(A) Conversion from String to Primitive types
You can use wrapper classes to convert string to primitive data types by using the
functions in the following ways:
⚫ String to Integer type data:
Syntax:
(a) integer variable = Integer.parseInt(String);
(b) integer variable = Integer.valueOf(String);
e.g., int n; String s = "24";
n =integer.parseInt(s);
or
n =Integer.valueOf(s);
Thus the variable n results in 24 (without quotes).

⚫ String to Long type data:


Syntax:
(a) Long variable = Long.parseLong (String);
(b) Long variable = Long.valueOf (String);
e.g., long n; String s = "2465783";
n =Long.parseLong(s);
or
n =Long.valueOf(s);
Thus, the variable n results in 2465783 (without quotes).
⚫ String to Float type data:
Syntax:
(a) float variable = Float.parseFloat (String);
(b) float variable = Float.valueOf(String);
e.g., float n; String s= "24.35";
n = Float.parseFloat(s);
or
n = Float.valueOf(s);
Thus, the variable n results in 24.35 (without quotes).

⚫ String to Double type data:


Syntax:
(a) double variable = Double.parseDouble (String);
(b) double variable = Double.valueOf(String);
e.g., double n; String s = "24.315467398";
n= double.parseDouble(s);
or
n= Double.valueOf(s);
Thus, the variable n results in 24.315467398 (without quotes).
Sumit Paul Notes

(B) Conversion from Primitive types data to String


A primitive type data can also be converted into String type.
⚫ Integer type data to String type
Syntax : String variable = Integer.toString(int);
Eg., int n = 24; Strign s;
S=Integer.toString(n);
Here, an integer type number (n=24) gets converted to a String.
Hence, s= “24”

⚫ Long type data to String type


Syntax : String variable = Long.toString(long);
Eg., long n = 246935; Strign s;
S=Long.toString(n);
Here, an integer type number (n=246935) gets converted to a String.
Hence, s= “246935”

⚫ Float type data to String type


Syntax : String variable = Float.toString(float);
Eg., float n = 24.35; Strign s;
S=Float.toString(n);
Here, an integer type number (n=24.35) gets converted to a String.
Hence, s= “24.35”
⚫ Double type data to String type
Syntax : String variable = Double.toString(double);
Eg., double n = 24.3452781; Strign s;
S=Double.toString(n);
Here, an integer type number (n=24.3452781) gets converted to a String.
Hence, s= “24.3452781”
Character type data
The character is defined as a letter, a digit or any special symbol enclosed within single
quotes. For example: ‘A’, ‘t’, ‘9’, ‘?’, ‘*’ etc

Assigning character literals to a variable


A character variable is declared under char data type to store a character literal.
A character variable is declared under char data type to store a character literal.
Syntax:
<character data type> variable = <Character literal>
e.g., char chr = ’A’;
char ch1 = ’8’;
char ch2 = ’&’;
The characters ‘A’, ‘8’ and ‘&’ are stored in variables chr, ch1 and ch2 respectively.
Sumit Paul Notes
Input a Character by using Scanner class
Scanner class is a member of java.util package. To input a character with the help of
Scanner class, use the following steps:

Step 1 : import the package java.util by using the following statement.


Import java.util. *;

Step 2 : Create a Scanner object to accept the character from the keyboard.
Scanner in=new Scanner (System.in)

Step 3 : Input the character by using Scanner object.


<char><variable>=<Scanner object>.<next().charAt(0)>;
e.g.,
char ch=in.next().charAt(0);

Character Oriented Functions


The Character data type is a component of character wrapper class of JCL. The character
wrapper contains character data type. It also includes some functions to manipulate the
character type data
Character ch = new Character ('a');
Following are the notable methods of the Character class.

1 Character.isLetter()
Determines whether the specified char value is a letter.

Character.isDigit()
2
Determines whether the specified char value is a digit.

Character.isWhitespace()
3
Determines whether the specified char value is white space.

Character.isUpperCase()
4
Determines whether the specified char value is uppercase.

Character.isLowerCase()
5
Determines whether the specified char value is lowercase.

Character.toUpperCase()
6
Returns the uppercase form of the specified char value.
Sumit Paul Notes
Character.toLowerCase()
7
Returns the lowercase form of the specified char value.

Character.toString()
8 Returns a String object representing the specified character value that is, a one-
character string.

Autoboxing
Autoboxing refers to the conversion of a primitive value into an object of the corresponding
wrapper class is called autoboxing.

Primitive Data Type Autoboxing Wrapper Object

Syntax: <wrapper class> <object> = new <Wrapper class> (Primitive Value)

Integer val = new Integer (25);

Wrapper Object Primitive Data

The integer type data 25 is converted into an object val of Integer wrapper class.

Unboxing
Converting an object of a wrapper class (Integer) to its corresponding primitive (int) value is
called unboxing.

Wrapper Object Unboxing Primitive Data Type

Syntax: <Primitive Data Type> <Variable> = <Wrapper Object>

Integer val = new Integer (75);


int y = val ;

Primitive Data Wrapper Object

//Programs to illustrate Autoboxing and Unboxing


Class Autoboxing
{
Public static void main(String args[])
Sumit Paul Notes

{
Int x=25, y;
Integer val = new Integer(x);
Y=val;
System.out.println(“Value after autoboxing\t” +val);
System.out.println(“value after unboxing\t” +y);
}
}

You might also like