Predefined Packages Java API

Predefined Packages Java API

Definition and Importance

A package is Java's style of bundling classes together. A package is a collection of related classes and interfaces. A package does not mean only predefined classes; a package may contain user defined classes also. A package is equivalent to a header file of C-lang. Packages can be compressed into JAR files for fast traversal in a network or to download from Internet.

Advantages of Packages

Like header files, packages come with many advantages.

  1. With a single import statement, all the classes and interfaces can be obtained into our program.
  2. Unlike a header file, Java permits to import even a single class also.
  3. Avoids namespace problems. Two classes of the same name cannot be put in the same package but can be placed in two different packages.
  4. Access between the classes can be controlled. Using packages, restrictions can be imposed on the access of other package classes. Access specifiers work on package boundaries (between the classes of other packages).
  5. We can find all the related classes and interfaces in a single space. Searching and identification will be easier.
  6. Packages and sub-packages are the easiest way to organize the classes.

Importing All/Single Class

Packages have an advantage over header files of C-lang. A package allows importing a single class also instead of importing all. C-lang does not have this ease of getting one function from a header file.

import java.net.*; // imports all the classes and interfaces
import java.awt.evnet.*; // imports all the classes and interfaces

import java.net.Socket; // imports only Socket class
import java.awt.event.WindowEvent; // imports only WindowEvent class

Note: While importing a single class, asterisk (*) should no be used.

Resolving Namespace Problems

By placing the same class in two different packages, which Java permits, namespace problems can be solved. Namespace is the area of execution of a program in RAM. The Date class exists in two packages – java.util and java.sql. Importing these two packages in a program gives ambiguity problem to the compiler. In the following program compiler gets ambiguity problem and is solved with fully-qualified name.

Fully Qualified Class Name

In the above program, if comments are removed, it is a compilation error as JVM is unable to judge which class should be given to the programmer. To solve the ambiguity problem, the class name is given along with package name and is known as fully qualified class name. Here, java.util.Date is known as fully qualified name. Another advantage of fully qualified name is that we can know to what package the class belongs immediately on the spot.

Note: When fully qualified name is included, importing the package is not necessary.

Package Naming Conventions

Like identifiers have conventions, the packages come with their own naming conventions. Like keywords and protocols, packages are also of lowercase letters. In a single project, a number of programmers may be involved assigned with different modules and tasks. To avoid namespace problems in storing their work, naming conventions are followed. While creating packages, they may follow company name, project name or personal name etc. to precede their package. Following are a few examples.

  1. jyothi.solutions: solutions is the package (folder) preceded by individual name, jyothi.
  2. forecastingtool.jyothi.solutions: Preceded by the project name(forecastingtool) and individual name.
  3. mindspace.forecastingtool.jyothi.solutions: Preceded by company name (mindspace), project name and individual name.

This package naming convention never clashes with the others work in a big project involving many programmers. The same convention is also followed in working on a domain also.

Java Class Libraries – : Predefined Packages Java API

All the classes and interfaces that come with the installation of JDK are put together are known as Java API (Application Programming Interface). All the Java API packages are prefixed with java or javax. Following table gives some important packages, a few prominent classes and their functionality.

Following table gives frequently used Predefined Packages Java supports

Random, Date, GregorianCalendar and the DS like Stack,
Vector, LinkedList, HashMap etc.

Package Name Example Classes Functionality (Purpose)
java.lang System, String, Object, Thread, Exception etc. These classes are indispensable for every Java program. For this reason, even if this package is not imported, JVM automatically imports.
java.util These are called as utility (service) classes and are used very frequently in coding.
java.io FileInputStream, FileOutputStream, FileReader, FileWriter, RandomAccessFile, BufferedReader, BufferedWriter etc. These classes are used in all I/O operations including keyboard input.
java.net URL, ServerSocket, Socket, DatagramPacket, DatagramSocket etc. Useful for writing socket programming (LAN communication).
java.applet AppletContext, Applet, AudioStub, AudioClip etc Required for developing applets that participate on client-side in Internet (Web) programming.
java.awt Button, Choice, TextField, Frame, List, Checkbox etc. Essential for developing GUI applications.
java.awt.event MouseListener, ActionListener, ActionEvent, WindowAdapter etc. Without these classes, it is impossible to handle events generated by GUI components
java.sql DriverManager, Statement, Connection, ResultSet etc Required for database access in JDBC applications.

Table: Predefined Packages Java Java API

Package vs Directory

The package of Java, at execution time, is converted into a directory (folder) by the operating system. The java.util is converted as java\util and java.awt.event is treated as java\awt\event by the OS. The asterisk * is a wild character of Windows which means all the files (means all the classes).

9 thoughts on “Predefined Packages Java API

    1. sasi

      Java API packages are predefined.
      User-defined packages are defined by the user to combine all the classes and Interfaces for performing a particular task.

  1. Akshay

    Sir,this is the best site for java,that i have even visited.it’s not possible for me to study from net all the time.so,can i get the pdf or any file version of all your notes,so that i could take printout of notes.That will be beneficial for me.sir,please help me if you can.
    Thankzz in advance sir

Leave a Reply

Your email address will not be published. Required fields are marked *