Construction. import declarations

[comp1110-75]

Writing a program that does something new and amazing is great! But even in that best-case scenario, you will usually need your program to execute many little steps that are very common.

The most common of those are part of the core of your programming language, like numbers, Strings, and basic arithmetic operations. Other common things are often contained in a Standard Library, usually part of a standard language distribution, as in the case of the Java Development Kit (JDK) which you installed as part of our standard software setup. Finally, other people and organizations may publish their own libraries of code that you may be able to include in your own program.

Java's import declaration lets you "import" a library (or parts of it) so you can use its functionality in your code. You typically put them at the top of any code file that uses them. For example, to import the array-related part of the java.util library, you would write:

import java.util.Arrays;

You can also import everything in a particular library using * as the last part of your imported library path, as in:

import java.util.*;

This may look convenient, but can easily cause name clashes and other maintenance issues, and as such is usually not a good idea.

bars magnifying-glass caret-down plus minus arrow-right xmark