Java Package
Java API built-in packages
User Defined custom packages
A java package is a group of similar types of classes, interfaces and sub-packages . Package is used to categorize the classes, easy distribution and provide access protection.
Java has built-in packages such as java, lang, System, awt, javax, swing, net, io, util, sql etc.
User-defined packages/custom packages are created specific to a project to contain all its classes, interfaces etc.
Packages are a part of a class name and they are used to group, distinguish named entities from other ones. Another purpose of packages is to govern code access together with access modifiers. For example, java.io.InputStream is a fully qualified class name for the class InputStream which is located in the package java.io.
A package is declared at the start of the file with the package declaration:
Built in packages
Java API
• Java.io – contains classes to support input/output operations
• Java.applet – contains classes for creating applets.
• Java.net – contains classes for supporting network operations.
Access packages by import keyword
import java.lang.Object;
import java.util.Random;
import java.util.*;
Custom packages
We can create our own packages to categorize all project files. These are user defined for a specific project/purpose/functionality.
We can import entire package or specific classes as per requirement. These packages can be distributed as required.
Add a class to an existing package: We can create our own packages to categorize all project files
A package created inside another package is sub package
import java.net.*;