Updated March 18, 2023
Introduction to Java Package
Packages in Java are of various kinds. A package is first introduced in Java to ensure that the functions or the basic operations related to the package are carried out very smoothly, and thus the operations are implemented using various Java tools and functionalities. Packages in Java are a group of classes that are interrelated, and they are used for some functions in the Java programming language. Each package in Java has a separate naming convention, and then they are subdivided into namespaces and conventions, which are used in the carrying out of functions inside a class which is varied. Packages are basically used for several functions within a Java programming language. Some of the reasons why a package is used are listed below:
- Preventing naming misconceptions such as two classes can have the same name, but their functionalities may be different.
- Making the searching, usage, and annotations of classes being used in the programming language easier.
- Packages are considered as data encapsulation or data hiding.
- Providing controlled access to the classes, which are either protected or private by default. 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 Package
Package names and directory names have the same working structure. If a package name is a school. teacher. maths then under the package name school there are sub-packages known as teacher and maths. The classes are easy to locate; that is the basic reason why the naming convention of packages is made similar to that of a directory. Packages inside a package are known as subpackage. They are not accessible by default. However, they have to be called separately to ensure that they are called at the time of object creation.
An example of a java subpackage created inside a Java package is given below.
Code:
import java.util.*;
Code Explanation: In the above line of code, we import, or we call, the Java package. Inside the java package, we have the util sub-package, which is also called. The full form of util is Utility. And all the classes within the package, as well as the sub-package, are called to ensure that the basic functionality of the program is implemented. There are many packages and sub-packages which are called at the time of object creation. In this article, we see a single example of a Java package that is being called.
There are built-in packages and user-defined packages inside the Java programming language. Some of the built-in packages which are present are listed below:
- 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.
Coding 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 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
In this article, we see the different kinds of user-defined packages as well as inbuilt packages that are present in the Java programming language. We also see an example of a piece of code where the java. Io. * package is implemented. The basic functionality of the java.io.* is to make sure that the classes for implementation of the input/output operations are called, which will ensure the smooth receiving of data from the user as input. Packages in Java are of various types. There can be numerous examples of packages that can be called inside the Java programming language for the implementation of various kinds of functions and classes.
Recommended Articles
This is a guide to the Java Package Example. Here we discuss the basic concept, working of the java package along with the example and code implementation. You may also look at the following articles to learn more –