The role of package, import, jar in java

1. package

1.1

The package name is like a real surname, and the class name is like a real name. The affiliation relationship between package and package is connected with ".", which is like double surname. For example, java.lang.String is a class whose surname is java.lang and whose name is String; java.io.InputStream is a class whose surname is java.io and whose name is InputStream.
This way of package greatly reduces the naming conflicts between classes with the same name but different packages.

2. import

2.1

import is at the beginning of the java file, first explaining which categories will be used.
Then we can use only the class name to specify a class in the code, that is, only the first name, not his last name.
Single-type import (single-type-import)
(example: import java.util.ArrayList; )
on-demand type import (type-import-on-demand)
(example: import java.util.*;)
has the following attributes:

Java imports any public class and interface in the package in two ways (only public classes and interfaces can be imported)

It is mentioned above that the import declaration only imports the classes under the declaration directory and does not import subpackages, which is why they are called type import declarations.

The simple name of an imported class or interface has compilation unit scope. This means that the type short name can be used anywhere in the compilation unit where the import statement is located. This does not mean that you can use the short name of all members of the type, but only the short name of the type itself.
For example: The public classes in the java.lang package are automatically imported, including the Math and System classes. However, you cannot use the short names of their members PI() and gc(), but must use Math.PI() and System.gc(). What you don't need to type are java.lang.Math.PI() and java.lang.System.gc().

Programmers sometimes import the current package or the java.lang package, which is unnecessary because members of the current package are themselves in scope, and the java.lang package is automatically imported. The java compiler will ignore these redundant import declarations (redundant import declarations). Even if
import java.util.ArrayList;
import java.util.*;
is imported multiple times like this, it can be compiled and passed. The compiler ignores redundant import declarations.

3. jar

3.1general

The jar package is to package the written classes. We can use the classes, properties and methods in these jar packages by putting them in the lib directory.

3.2 occupation

JAR file is the abbreviation of Java Archive File-java archive file. It is a platform-independent file format. Based on the zip file format, many files are combined into a compressed file. jar. The difference is that there is one more META-INF/MANIFEST than zip. .MF file, which is created automatically when the JAR file is generated.

3.3 function

JAR files are used not only for compression and distribution, but also for deployment and packaging of libraries, components, and plug-ins, and can be used directly by tools like compilers and the JVM. Include special files in the JAR, such as manifests and deployment descriptors, which instruct tools how to handle a particular JAR.
JAR files are especially useful when it comes to Internet applications. Before the JAR file, the Web browser must repeatedly request the Web server to download all the files that constitute an "applet" (Applet); The server makes one request. In addition, each file is uncompressed, and thanks to the compression technology, all data can be obtained in a shorter time.

Reference https://blog.csdn.net/qq_25665807/article/details/74747868

3.4 Use the classes in the jar package in the project

1. Import the jar package into the project first, put it under the lib file, and set the path.
2. When using it, import the corresponding jar package

Suppose we need to use the class org.demo.jar contained in the test.jar file, and
the source of the class is Myjar.java in the file
:


import org.demo.*;

public class Myjar {

    public static void main(String[] args) {

    ......

   }
}

1. First, in your code, you have to import the class. To do this, you need to import org.example.Classname;
2. When compiling the source code, the jar file must be referenced. Note the difference when compiling using : and ;
Under Windows: javac -cp .;org.demo.jar myjar.java
After this, you will get the bytecode file myjar.class Now you can run this:
Under Windows :java -cp .;org.demo.jar myjar

Guess you like

Origin blog.csdn.net/weixin_44313315/article/details/126888228