Package is a collection of predefine classes and interface. Java provide different type of inbuilt packages for different task. Programmer can also create own packages. All Java predefine classes are distributed into several different package.
Some package are:
- lang Package: Lang stands for language. This package contain those class which are essential for every java program.
E.g. String and System class
Lang is the default package of java which means it is optional to import Lang package in program
Syntax of Import package:-
import [package name].*; // (all class)
import [package name].[class name];//(Single class)
- io package: io stands for input/output. This package contains those class which are essential for input and output. Some classes of input/output package are;
E.g. – DataInputStream, BufferReader, DataInoutStramReader
- util package: util stands for utility package this package contain different type of classes related to various task.
E.g.: Date, time, calendar, array, vector. Array List etc.
- awt package (swing): awt stands for abstract windowing tool. With the help of this package we can create GUI interface. This package provide many GUI tools.
E.g.: Command button, choice, list, radio, etc.
awt contain a sub package name “even”.
- applet: with the help of applet function we can create a java program which can be embedded into HTML page by which web browser execute the java program. Web browser must be java enabled.
- Sql Package: sql stands for structure query language. This package provides a complete range of classes which is needed for jdbc (java data base connectivity). Some important classes of sql package are:
E.g.: connection, ResultSet, PrepareStatement, etc.
- Net package: net stands for networking. This package contains classes to implement or create connection between two or more systems or if we want to implement client server approach then. We can use the classes of net package.
E.g.: TCP/IP, Request, etc.
How to create our own package:
- First create a directory within name of package.
- Create a java file in newly created directory.
- In this java file you must specify the package name with the help of package keyword.
- Save this file with same name of public class
Note: only one class in a program can declare as public. - Now you can use this package in your program.
Package Pack1:
- public class Demo
- {
- public void Show()
- {
- System.out.print(“Package called”);
- }
- }
After that use this package in your program,
- import Pack1.*
- class A
- {
- public static void main(String …args)
- {
- Demo ob1= new demo();
- ob1.Show();
- }
- }
Read more articles on Java: