You are reading the article Comprehensive Guide To Java Package updated in September 2023 on the website Nhahang12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Comprehensive Guide To Java Package
Introduction to Java PackageWeb development, programming languages, Software testing & others
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 PackagePackage 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.
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 PackageIn 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
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); } }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.
ConclusionIn 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 ArticlesThis 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 –
You're reading Comprehensive Guide To Java Package
Update the detailed information about Comprehensive Guide To Java Package on the Nhahang12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!