What are Java packages?

A package is a group of associated types, interfaces, enumerations, and annotations that provide access control and namespace management. Packages are classes that contain methods but do not have the main method. A package is a collection of classes, interfaces, and sub-packages in Java. It’s similar to a folder in a file directory.

Why use packages?

What are the benefits of using packages?

  1. The Java package is used to organize the classes and interfaces to make them easier to maintain.

  2. Access control is provided via the Java package.

  3. A Java package eliminates naming conflicts.

Packages are mainly divided into two forms. They are:

  1. Built-in packages.
  2. User-defined packages.
TYPES OF PACKAGES IN JAVA

What are built-in packages?

Built-in packages are provided for the program to reduce the burden. There are various in-built packages accessible in Java. Some of the built-in packages in Java are:

  • Java.util: Holds the collection structures, some of these packages support classes, properties, random number generation classes. Classes like HashSet, LinkedList, Queue, HashMap, Collection, Arrays, Date, Optional, etc., are part of this package.

  • Java.io: Produces classes for system input/output operations. Classes like BufferedInputStream, BufferedReader, CharArrayWriter, BufferedWriter, FileReader, InputStream, OutputStream, PrintStream, PipedReader, Serializable, etc., are part of this package.

  • Java.lang: Provides classes and interfaces that are rudiments to the design of Java programming language. Classes like Float, Boolean, Integer, Long, String, StringBuffer, StringBuilder, etc are part of this package.

  • Java.sql: Accommodates the classes and interfaces for obtaining and processing data stored in a database. Classes or interfaces like Struct, PreparedStatement, Wrapper, Connection, DriverAction, DriverManager, Driver, ResultSet, Statement, etc are part of this package.

These built-in packages are stored in the package java.

To use these classes/interfaces present in these packages, we need to import them into the ongoing program.

The syntax for importing packages is:

import java.util.*

The above statement instructs the compiler to import all classes found in the util package, which is found in the java package.

The asterisk symbol instructs the compiler to import all the classes into the present in the util package. If we want to import a specific class, then we need to mention the name of the class instead of the asterisk.

Example:

import java.util.Scanner

In the example:

  • java is a top-level package

  • util is a sub-package

  • Scanner is a class present in the sub-package

Note: The package java.lang is spontaneously imported to every program that we write. That is why we don’t need any import statement in our programs for using classes like

  • Float
  • StringBuilder
  • Integer, etc.

Aside from java.lang, other packages must be imported into your program to use the classes and interfaces accessible in that packages.

What are user-defined packages?

User-defined packages are the packages created by the users. Users are free to create their packages. To create your package, you need to understand that Java uses a file system directory to store them, just like folders and sub-folders on your computer.

How to create/write packages in Java

We use the package keyword to create or define a package in Java programming language.

  1. The package keyword must come before the package name.

  2. The package name must be a solitary word. For example ‘my package’ should be written as ‘mypackage’. There should be no space between my and the package.

  3. The package name should use lower case notation.

  4. The package statement should be the first line of statement in the program.

Syntax:

//Create a Class
class Demo {
//Creating a method to print "This is my package"
public void method() {
//Print out "This is my package"
System.out.println("This is my package!");
}
//Main Method
public static void main(String[] args) {
//Creating an instance of the first class
Demo obj = new Demo();
obj.method();
}
}

Note: Package names are written in all lowercase to avoid conflict with the names of classes or interfaces.