What are library classes and library functions in Java
Library Classes:- These are the predefined classes provided by Java system present in packages. These classes contain related library functions like String Math, etc. All the library classes belong to some package.
Library Functions:- These are the inbuilt functions present in Java library classes, provided by Java system to help programmers to perform their task in an easier way.
Library Classes are to be included in java program using a package.
Package:-Packages are collection of classes or subclasses. We have different packages defined to import or include the classes required in the Java program.
The different packages are:
1. io package:- It includes all the classes related to input/output.
2. lang package:- It include all the statements and functions common to all program.For eg we have,System.out.println ,main function etc used in all the program.Also lang package is the default package which is by default imported in all the java program.But we can also include this package explicitly.
3. util package:- It includes all the utility classes.For eg like date and time.
4. awt package:- It includes all the classes related to graphical design.
5. sql package:- It includes classes of the database connectivity.
6. applet package:- It includes all the classes related to Internet programming.
7. net package:- It includes all the classes related to networking.
This statement is used to add or include the classes related to the package. Import statement should be the first statement of the program.
How to import a package:
Program to take the input from the user to print the Name and Age of the Employee.
We use BufferedReader class which is in io package to take the input and read it and print the output on the screen.
import java.io.*; // package imported for input output classes
public static void main(String args)throws IOException // *
InputStreamReader ir=new InputStreamReader(System.in); // function to take input from user.
BufferedReader br=new BufferedReader(ir); // class to read user input.
System.out.println("Enter the Name");
name=br.readLine(); // function defined in buffered class to take input from user as a string
System.out.println("Enter the Age");
age=Integer.parseInt(br.readLine()); // converting default string input to integer
System.out.println("Name is " + name);
System.out.println("Age is "+ age);
*Exception occurred when we import the io package .So we use throws Exception to deal with the exception.So that it will not create error in executing the program.
Incoming terms: library classes and library functions