Java Packages
Java Packages
Packages
• It is a logical organization of related classes. We can logically organize java classes into
packages.
• It means putting all the related classes in one place that is a package.
• Ex: In a banking application we put all the accounting related classes in one package.
• They must be accessed through their package names.
why do we need them?
In programming it is often helpful to group related pieces of a program together, and it is accomplished
using packages.
In java, programmers can create several classes and Interface. After creating the classes and interface, it
is better if they are divided into some groups depending on their relationship.
Thus, the classes and Interface which handle similar or same task are put into the same directory or
folder, which is also known as package.
Packages acts as an “container” for classes. A package represents a directory that contains related group
of classes and Interface.
Classes defined within a package can be made private the package so that is cannot be accessed by code
outside the package. Thus it provides classes encapsulation.
Types Of Packages
There are basically only 2 types of java packages.
They are as follows :
• Java API Packages
• User defined Packages
Java API Package
It contains all the built in classes provided by java. They are organized as the collection of classes
into packages based on their functionality. These classes inside the packages are already defined
and we can use them by importing relevant packages in our program.
It is easier to remember name of the package rather than classes.
Java has an extensive library of packages, a programmer need not think about logic for doing any
task. For everything, there are methods available in java and that method can be used by the
programmer without developing the logic on his own. It makes programming easy
Java API Packages And Their Classes
• Java.lang : Language Support classes. These are the classes that java compiler itself uses and
therefore they are are automatically imported. They include classes for primitive types such as
strings, maths function, threads and exeptions.
• Java.util : Language utility class such as vector, hash table, random numbers, date, scanner etc.
• Java.io : Input/Output support classes. They provide facilities for the input and output of the
data. Some classes are Bufferedreader, Bufferedwriter, ByteArrayInputStream etc.
• Java.awt : Set of classes for implementing graphical user interface. They include classes for
windows, buttons, list, menus and so on.
• Java.net : Classes for networking. They include classes for communication with local computers
as well as with internet servers. Some of the classes are URL class, URLConnection class, socket
class and so on.
• Java.applet : classes for creating and implementing applets , some of the classes are
Using API packages
Every time we use want to use a package we call it using import command.
When we uses import command we refer to a class by its full package name.
Ex: (i) import java.util.Vector; // to get a specific class
Vector v = new Vector();
(ii) import java.util.*; // use asterisk(*) to get all the classes in the package
Scanner sc = new Scanner();
Vector v = new Vector();
import java.util.*;
import java.io.*;
import java.lang.Math;
Step 2 : place the file packageExample.java in C:\ and compile the program. We have to use –d option to create the