A package in Java is a mechanism to organize classes, interfaces, and other related components into a single namespace. It helps in avoiding naming conflicts and provides a structured way to manage and organize Java code. Just like folders in a file system, packages group related classes together. They are used to create a hierarchical structure for organizing Java code.
Syntax of Package:
In Java, the package statement is used to declare that a particular class belongs to a specific package. The package statement appears as the first line in a Java file before any import statements or class declarations. The syntax for declaring a package is as follows:
package com.mydomain.mypackage;
Here's what each part of the package statement means:
- com: It represents the top-level domain or company domain. It is usually written in reverse order to ensure uniqueness, such as `com`, `org`, `net`, etc.
- mydomain: It represents the domain or subdomain within the company's domain. It is used to further identify the organization or project.
- mypackage: It represents the specific package within the domain that will contain the classes.
Built-in Packages:
Java comes with several built-in packages, which are also known as the standard Java libraries or core packages. These packages contain a wide range of useful classes and interfaces that provide functionalities like I/O operations, collections, networking, GUI, etc. Some of the commonly used built-in packages include:
- `java.lang`: Contains fundamental classes and is automatically imported into every Java program. For example, `String`, `Object`, etc.
- `java.util`: Contains utility classes for working with collections, arrays, dates, and more. For example, `ArrayList`, `HashMap`, etc.
- `java.io`: Contains classes for performing I/O operations, such as reading and writing to files. For example, `FileInputStream`, `FileOutputStream`, etc.
User-defined Packages:
Apart from built-in packages, Java allows developers to create their own custom packages to organize their code better and maintain a modular structure. User-defined packages are used to group related classes together, making it easier to manage and maintain a large codebase.
To create a user-defined package, you need to follow these steps:
1. Choose a unique package name: Generally, the package name follows the reverse domain name convention to ensure global uniqueness. For example, if your domain is `example.com`, you could choose `com.example.myapp` as the package name.
2. Create a directory structure: Create a directory structure that matches the package hierarchy. For example, if the package name is `com.example.myapp`, you would create a folder structure like this:
src/
└── com
└── example
└── myapp
3. Place your Java files in the corresponding directories: Place the Java files (classes or interfaces) that belong to the package in their respective directories. For example, if you have a class named `MyClass` belonging to the package `com.example.myapp`, you would place it in the `myapp` folder.
4. Add the package declaration: In each Java file that belongs to the package, add the package declaration at the top of the file. For example, for `MyClass`, you would add the following line:
package com.example.myapp;
Importing Packages and Classes:
To use classes from other packages in your Java code, you need to import them.
If you want to use a class from a package, you have two options:
1. Use the fully qualified name of the class, which includes the package name. For example:
java.util.Scanner scanner = new java.util.Scanner(System.in);
2. Or, you can import the class and then use just the class name. For example:
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
If you want to import all classes from a particular package, you can use the '*' wildcard:
import java.util.*;
Scanner scanner = new Scanner(System.in);