Java – Package

In this tutorial, we will learn about the java package. Here, we will learn what is package, how to use a package, and how to create a package in Java.

What is a package in Java?

In java, the package is just like a folder that includes classes, interfaces, and sub-packages also. It behaves like a library in Java and it stores pre-defined classes and interfaces in it.

How to Use Package.

In java, packages are referred to using the import keyword. We can call the class of the packages using the import keyword.

Example to import all classes of package.

import java.util.*;

Here, in the above example, in the end, we are using asterisk(*) sign. It defines that we can use any class or interface of that package.

Another way to use package

import java.util.Scanner;

Here, in the end, we are using the name of the class instead of the asterisk(*) sign. It defines that we can use only that class of the package in our program.

In the above example

  1. java is a top level package.
  2. util is a sub package of java package.
  3. Scanner is a class of util sub package.

Note: Using asterisk(*), we can call only classes and interfaces, but not package. You have to write package name there.

Some Important Built-In packages.

  1. java.lang – It is a default package in java, you need not to import this package. It includes primitive types, keywords and classes like System, String etc.
  2. java.util – It includes utility classe like Scanner, Date & Time and Collections.
  3. java.io – It stands for Input/Output, includes classes and interfaces for input and output.
  4. java.awt – It stands for abstract window toolkit, it includes classes for GUI programming. Like Button, Labbel, Menu etc.
  5. java.sql – It includes classes for Java database connectivity (JDBC).