Home > Java > Packages > Introduction
Introduction

Introduction to Packages

Packages in Java are a way of grouping related classes and interfaces. The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface", or "API" for short. Its packages represent the tasks most commonly associated with general-purpose programming.

 

The Java Platform API Specification contains the complete listing for all packages, interfaces, classes, fields, and methods supplied by the Java Platform 6, Standard Edition. Load the page in your browser and bookmark it. As a programmer, it will become your single most important piece of reference documentation.

 

At the beginning of each program file, you must place the import statement to bring in any extra classes you’ll need for the code in that file. Note that I say “extra;” that’s because there’s a certain library of classes that are automatically brought into every Java file: java.lang. Start up your Web browser and look at the documentation from Sun. (If you haven’t downloaded it from java.sun.com or otherwise installed the Java documentation, do so now). If you look at the list of the packages, you’ll see all the different class libraries that come with Java.

 

Select java.lang. This will bring up a list of all the classes that are part of that library. Since java.lang is implicitly included in every Java code file, these classes are automatically available. There’s no Date class listed in java.lang, which means you must import another library to use that. If you don’t know the library where a particular class is, or if you want to see all of the classes, you can select “Tree” in the Java documentation. Now you can find every single class that comes with Java. Then you can use the browser’s “find” function to find Date. When you do you’ll see it listed as java.util.Date, which lets you know that it’s in the util library and that you must import java.util.* in order to use Date.


A package is what you get when you use the import keyword to bring in an entire library.


For example:


import java.util.*;


This brings in the entire utility library that’s part of the standard Java distribution. Since, for example, the class ArrayList is in java.util, you can now either specify the full name java.util.ArrayList (which you can do without the import statement), or you can simply say ArrayList (because of the import).


If you want to bring in a single class, you can name that class in the import statement


import java.util.ArrayList;


Now you can use ArrayList with no qualification. However, none of the other classes in java.util are available. The reason for all this importing is to provide a mechanism to manage “name spaces.” The names of all your class members are insulated from each other. A method f( ) inside a class A will not clash with an f( ) that has the same signature (argument list) in class B. But what about the class names? Suppose you create a stack class that is installed on a machine that already has a stack class that’s written by someone else? With Java on the Internet, this can happen without the user knowing it, since classes.


Some packages are as folllows:



  • java.lang: The java.lang package is implicitly included in every Java code file, these classes are automatically available. Classes in java.lang are the only ones in the Java API that do not require an import declaration This package is imported by the compiler into allprograms, so you do not need to do so.
  • java.awt: The Java Abstract Window Toolkit (AWT) Package contains the classes and interfaces required to create and manipulate GUIs in Java 1.0 and 1.1. In current versions of Java, the Swing GUI components of the javax.swing packages are often used instead.
  • java.swing: The Java Swing GUI Components Package contains classes and interfaces for Java’s Swing GUI components that provide support for portable GUIs.
  • java.io: The Java Input/Output Package contains classes and interfaces that enable programs to input and output data.
  • java.util: The Java Utilities Package contains utility classes and interfaces that enable such actions as date and time manipulations, randomnumber processing (class Random), the storing and processing oflarge amounts of data and the breaking of strings into smaller  pieces called tokens (class StringTokenizer).
  • java.applet: The Java Applet Package contains a class and several interfaces required to create Java applets—programs that execute in Web browsers.

Leave a Reply

Fields with * are mandatory.
* Name:
Website:
* Comments:
 
Tags allowed: <strong> <i> <u>
* Code:
 
 
Submit   Cancel