Java packages
A package is a group of related classes and interfaces that work together to provide a wide variety of functionality for various purposes.
This tutorial focuses on:
- Different Java packages
- Importing packages
Different Java packages
Java provides many different packages:
- java.lang - Provides classes that are fundamental to developing Java programs including the String class (for working with text strings) and the Math class (for performing calculations and for working with numeric data). Imported by default into all Java programs.
- java.io - Provides classes for input and output for things like interaction with the user and reading and writing to files.
- java.awt - Provides classes for creating graphical user interfaces, drawing graphics, and displaying images.
- java.awt.event - Provides classes and interfaces for handling events like minimizing of a window, clicking a button, and moving the mouse.
- java.net - Provides classes for creating communication and exchanging data over a network.
Importing packages
To be able to use classes and interfaces located in a package, you have to import the package they are in. If you try to use a class or interface without importing the package it is located in, you will get an error. Packages are imported using the import keyword.
Three import methods:
Import an entire package - This means you will be able to use all the classes and interfaces in that package. To do this use the package name followed by the * character.
Import individual classes - You will be able to use just those classes.
Import individual interfaces - You will be able to use just those interfaces.
NOTE: The import statement should be the first thing in your code, even before the class declaration!