Home > Java > Javagetting Started > What is the java package?

What is the java package?

(*-*)浩
Release: 2019-11-12 10:39:07
Original
2629 people have browsed it

What is the java package?

A package is a container for classes, used to separate class name spaces. If no package name is specified, all examples belong to a default unnamed package.

Packages in Java generally contain related classes. For example, all classes related to transportation can be placed in a package named Transportation.

What is the purpose of the package?                                                                                                                                                                                                                                                                                                   hierarchical relationship.

How to create a package?

If you don’t use IDE tools, creating a package means creating a folder. After creating a new class, just add the package name at the beginning of the class. If you use IDE tools, it will be more convenient to create a new package and specify the package name. That’s it.

Programmers can use package to indicate which specific package the class in the source file belongs to.

The format of the package statement is:

package pkg1[.pkg2[.pkg3…]];
Copy after login
If there is a package statement in the program, the statement must be the first executable statement in the source file, and it can only be preceded by comments or blank lines. In addition, there can be at most one package statement in a file.

The names of packages have a hierarchical relationship, and each layer is separated by dots. The package hierarchy must be the same as the file system structure of the Java development system. Usually, all lowercase letters are used in package names, which is different from the naming convention in which class names start with uppercase letters and the first letter of each word is also capitalized.

When using package declarations, there is no need to reference (import) the same package or any elements of the package in the program. The import statement is only used to introduce classes from other packages into the current namespace. The current package is always in the current namespace.

If the file is declared as follows:

package java.awt.image
Copy after login

, then this file must be stored in the java\awt\image directory of Windows or the java/awt/image directory of Unix.

In a nutshell, the main reason for the introduction of "packages" in Java is the need for cross-platform features of Java itself. Because all resources in Java are also organized in files, which mainly include a large number of class files that need to be organized and managed.

Java also uses a directory tree structure. Although the management of files on various common operating system platforms is organized in the form of directory trees, they express directory separation in different ways. In order to distinguish themselves from various platforms, Java uses "." to separate directories.

The above is the detailed content of What is the java package?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template