next up previous
Next: What's the deal with Up: Packages Previous: Packages

What's a package, anyway?

If you're using an IDE, some of the terminology here will not seem as appropriate as if you were using the Java SDK directly (i.e. the commands javac and java issued at the command line), but the basic ideas should be the same.

In the previous section, we said that the String class definition can be found in the package java.lang. But what's a package? Well, remember that we also said that the String class isn't really part of the language? Since classes like String are used so much, the language is shipped with some additional classes; these classes are contained in packages. You can think of a package as something that contains classes and subpackages. The subpackages again can contain (sub)subpackages and classes, and so on, forming a hierarchy.. (This definition will change slightly later, but for now it's adequate)

What classes are provided, and what are the names of the packages? This information appears in the API. In order to easily use the members of these classes, the best thing to do is to use an import statement. This allows us to use simple names for the members of these classes, as opposed to fully qualified names. All this is best illustrated by the following example:

First of all, this is what it looks like if we don't do any importing:


public class iotestfq{ 
    
    private static  java.io.BufferedReader in;

    public static void Input(){
            java.io.InputStreamReader k = new java.io.InputStreamReader(System.in);
            in = new java.io.BufferedReader(k);
        }
    
    public static void main(String[] args) throws java.io.IOException{
	Input();
	String il = in.readLine();
	System.out.println("il = " + il);
    }
}

Notice that every time we access a class from the package java.io, we have to write the full name -- that is, the full package name plus the class name. These are called fully qualified names.

Now let's use an import statement:


import java.io.BufferedReader;
public class iotestpq{ 
    
    private static  BufferedReader in;

    public static void Input(){
            java.io.InputStreamReader k = new java.io.InputStreamReader(System.in);
            in = new BufferedReader(k);
        }
    
    public static void main(String[] args) throws java.io.IOException{
	Input();
	String il = in.readLine();
	System.out.println("il = " + il);
    }
}

This time we imported the class BufferedReader. This allows us to refer to this class with the simple name BufferedReader rather than the fully qualified name java.io.BufferedReader. Note that we still have to use a fully qualified name to refer to the class InputStreamReader.

In this final piece of code, we import everything in the package java.io using the statement import java.io.*;. Here * means 'all classes in the package'.


import java.io.*;
public class iotestfq{ 
    
    private static  BufferedReader in;

    public static void Input(){
            InputStreamReader k = new InputStreamReader(System.in);
            in = new BufferedReader(k);
        }
    
    public static void main(String[] args) throws IOException{
	Input();
	String il = in.readLine();
	System.out.println("il = " + il);
    }
}

Since both BufferedReader and InputStreamReader are classes in the package java.io, both can now be referred to using simple names. This is probably what you would do most often.

Why didn't we have to type import java.lang.String in order to use the String class? After all, we've been using it without a fully qualified name. The answer is that all the classes in the package java.lang are imported automatically, as if the declaration import java.lang.* appeared at the beginning of every program that we write.


next up previous
Next: What's the deal with Up: Packages Previous: Packages
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09