The content of this article is to introduce what are the packages in java? Why use java package. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What are the packages in java?
A package in Java is a mechanism that encapsulates a group of classes, sub-packages and interfaces and can be used to organize a group of related classes and interfaces. Conceptually, we can think of packages as similar to different folders on your computer: you can keep HTML pages in one folder, images in another, scripts, or scripts or applications. in another folder.
Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to stay organized by putting related classes and interfaces into packages.
There are two types of packages in Java: built-in packages and packages we create ourselves (also called user-defined packages)
Benefits of using java packages
This is the reason why Java packages are used:
1. Reusability. When developing projects in Java, we often feel that there is very little written in our code over and over again. Using a package you can create something like this as a class inside the package and whenever you need to perform the same task just import the package and use the class.
2. Used to classify classes and interfaces to better organize and maintain them. In our large Java projects with hundreds of classes, there is always a need to group similar types of classes into a meaningful package name so that you can better organize the project and quickly locate it and use it when needed , which improves efficiency.
3. Prevent naming conflicts. We can define two classes with the same name in different packages to avoid name conflicts, and we can use the package
Related knowledge of Java packages:
A package is a container for a set of related classes, some of which are accessible while others are reserved for internal purposes.
How does the package work?
Package name and directory structure are closely related. For example, if the package name is college.staff.cse, then there are three directories, college, staff and cse, so that cse exists in staff and staff is in college. In addition, the directory College can be accessed through the CLASSPATH variable, that is, the parent directory path of college exists in CLASSPATH. The idea is to make sure classes are easy to find.
Naming convention for packages: Packages are named in reverse order of domain names, i.e. org.geeksforgeeks.practice. For example, in college, the recommended conventions are college.tech.cse, college.tech.ee, college.art.history, etc.
Add classes to package: We can add more classes to the created package using the package name at the top of the program and save it in the package directory. We need a new java file to define a public class, otherwise we can add the new class to the existing .java file and recompile it.
Sub-package: A package within another package is a sub-package. They are not imported by default and must be imported explicitly. Furthermore, members of subpackages have no access rights, i.e. they are treated as different packages with protected and default access specifiers.
Package types in Java
There are two types of packages in java.
1. Built-in packages: Packages that have been defined in java, such as: java.io. *, java.lang.*, etc. are called built-in packages.
2. User-defined packages: The packages we create ourselves are called user-defined packages.
Simple example of Java package:
1. Create a package in java
We can use the package keyword in Java creation package.
//保存 Simple.java package mypack; public class Simple{ public static void main(String args[]){ System.out.println("欢迎来到包装"); } }
2. Compile the created package
If you do not use any IDE, you need to follow the following syntax:
javac -d 目录 javafilename
For example:
javac -d . Simple.java
javac -d . Simple.java
java mypack.Simple
The above is the detailed content of What are packages in java? Why use java package. For more information, please follow other related articles on the PHP Chinese website!