Table of Contents
- Introduction
- Working of Java Packages
- Types of Java Packages
- How to Create Packages in Java?
- Example of Java Package
Introduction to Java Packages
The following article, Java Packages, outlines the creation of Java packages. The package encapsulates a set of classes, interfaces, and sub-packages. Packages make the nomenclatures well-defined and closely associated with the coding design context, giving the developer a simple idea. Packages also helped control the data encapsulation, as the default and protected members of the class appear through the package scope only; they are not public to all classes. Before delving into the functioning of packages, let’s first define some key terminology. A subpackage is located within another package, such as in java.util.ArrayList, where Java serves as the parent package, and util functions as the subpackage.
Here are some reasons why people use a package:
- Preventing naming misconceptions such as two classes can have the same name, but their functionalities may be different.
- Making it easier to search for, use, and annotate the classes used in the programming language
- Consider packages as data encapsulation or data hiding.
- Convert the classes, which are either protected or private by default, into active voice and provide controlled access. Protected and private classes do not allow variables of other classes to be accessed easily as they are private to the class, and hence they cannot be accessed by member functions of all classes.
Working of Java Packages
The working structure of package names and directory names is the same. If a package name is “school.teacher.maths,” then there are sub-packages under the “school” package, named “teacher” and “maths.” Locating classes is straightforward, which is why the naming convention of packages closely resembles that of directories. Subpackages inside a package are referred to as “subpackages.” They are not automatically accessible; however, you need to call them individually to ensure they are invoked during object creation.
Here’s an example of creating a Java subpackage inside a Java package.
Code:
import java.util.*;
Code Explanation: The above line of code imports the Java package or calls it. Inside the Java package, we have the util sub-package, which is also called util. The full form of util is Utility. We call all the classes within the package as well as the sub-package to ensure that we implement the basic functionality of the program. We call many packages and sub-packages at the time of object creation. In this article, we provide a single example of a Java package being called.
There are built-in packages and user-defined packages inside the Java programming language. Some of the built-in packages that are as follows:
- Java.lang: Contains classes for implementing language operations.
- Java.io: Contains classes for supporting input/output operations.
- Java.util: Contains classes for supporting linked list, stack, queue, etc.
- Java.applet: Contains classes for implementing basic applets in Java.
- Java.awt: Contains classes for accessing buttons, menu, etc.
- Java.net: Contains classes for supporting network applications.
There are also user-defined packages inside the Java programming language. Here, we create a directory first, and then we implement the working of the package inside the programming tree.
First, we create the name of the directory, and then we type the name of the package that has to be created. Once the package is created, we can create names of sub-packages within the created package as well. This forms the basis for calling the different classes present inside the Java programming language.
Types of Java Packages
- Java offers the flexibility to use built-in Java packages or user-created packages based on the use case.
- The built-in packages are always crucial while coding, as they offer a lot; the rt.jar file carries multiple functionality definitions, which appear in the java.util.* like packages.
Now let us see built-in and user-defined packages in detail –
1. Built-in Packages
Built-in packages contain a large number of Java classes, and it contains the following packages –
- lang – This package automatically imports and encapsulates the primary classes, including the Object class.
- Utilise – This is a crucial package and contains many classes related to collections like ArrayList, HashMap, etc. All the data structure implementations are in this class, and you need to use them by incorporating them abstractly.
- io – Place the input-output stream handling and processing-related classes in this package. Some examples of such classes include InputStreamReader, FileReader, and so on.
- net – This contains the classes used for performing certain networking-related operations; the example classes are Socket and SocketAddress.
- beans – Contains classes related to bean development and components based on Java beans architecture.
2. User-Defined Packages
- A user always has the privilege to enclose his created classes into some package; the user can only define that package’s name and directory structure in his custom way.
- Programmers place a package in the default package when it has no assigned class.
Example
package com.supplychains
class SupplyChainManagement
{
public void getPrompt()
{
System.out.println("Welcome to SCM");
}
}
Other classes can now access this class by simply importing the “com.supplychains” package. Once the package is imported, you can access the class “SupplyChainManagement” and its member functions and variables.
How to Create Packages in Java?
First, you should have a class; let us consider only the abovementioned class structure.
package com.supplychains
class SupplyChainManagement
{
public void getPrompt()
{
System.out.println("Welcome to SCM");
}
}
We will save this class with the name “SupplyChainManagement.java,” as we intended.
- Now compile this file with a javac compiler, which can be done by writing javac SupplyChainManagement.java; this will create a .class file in the same directory.
- Now we can use the command, “javac -d. SupplyChainManagement.java”, this command will result in package formation, now directory structure is a thing we have to be keen about; the “.” placed after -d in the above command represents the current working directory. The program will create a folder in the selected directory, form a package within that folder, and place the class file created in Step 2 within this package.
- The next step is to compile the package; this can be done with the following command –
"javac -d .. SupplyChainManagement.java. "
.. represents the parent directory (like C drive or D drive).
- Hence, you can organize and bundle multiple classes in a directory structure, ensuring you can only access them in the corresponding order.
- Now you need to use an import statement to incorporate this package in any Java classes; note that Java runtime will refer to it concerning the path set in the environment variable, which contains the root directory only.
Example of Java Package
In the coding example, we will see a simple program and its output, which will help us understand the import of packages that are present in the Java programming language. In this program, we are going to calculate the simple factorial of a number using only one function. The factorial of a number is the number multiplies with all its digits less than itself up till 1. An example of a factorial of a number is
3!= 3*2*1= 6
4!=4*3*2*1= 24
And so on…..
Only the import java.io.* package is called. It is used to call classes that help in input/output operations.
Code:
import java.io.*;
class Factorial {
public static void main(String args[]) throws IOException {
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter any number : ");
int N = Integer.parseInt(stdin.readLine());
int i;
double fact = 1;
i = 1;
while (i <= N)
{
fact = fact * i;
i++;
}
System.out.print("The factorial of " + N + " is " + (double)fact);
}
}
Output:
Code Explanation: In the sample output, we see the factorial of 7, which comes to 5040. We can also calculate the sum of factorials of numbers up to 100 or any other number. However, the last digit of the sum of factorials of a number will always be 3 whenever there is a calculation of a sum of factorials of a number more than 5. An example of a sum where we calculate the last digit of the sum of factorial till 8 factorial.
The sum of 1! + 2! + 3! + 4! + 5! + 7! + 8!. We want to find the last digit of the sum. Now, we calculate the sum of factorials up to 5! Because after that, the last digit is 0. So the sum is 1(1 !) + 2(@ !) + 6(3 !) + 24( 4! ). So the last digit comes out to 3. This is a very important concept in the number system.
Conclusion
Hence, we read a little about packages in Java, their creation, their working, and how we can create and import our packages from anywhere to any other classes. Packages can encapsulate the interfaces and classes. A wide variety of built-in packages are already available to exploit the data structure and algorithms; Java provides a wide variety and supports multithreading through multiple concurrency packages.
Recommended Articles
This is a guide to Java Packages. Here we discuss the introduction, working, and types of the package, which include built-in and user-defined packages and the creation of packages in Java. You may also look at the following articles to learn more –