Java Class Library and Organizational Structure (Java API)
Java official provides developers with many powerful classes. These classes are placed in each package and released with the JDK, called Java class library or Java API.
API (Application Programming Interface) is a general concept.
For example, I wrote a class that can obtain various hardware information of the computer. It is very powerful and stable. If your project also needs such a function, then you no longer need to write code by yourself. Use my class directly Can. However, my class code is very complicated, and it is not realistic to let you read these codes, and I don’t want you to see my code (you don’t have to or don’t want to understand these obscure codes). I want to protect me What should I do with the copyright?
I can first compile my class and accompany a document to tell you how to use my class and what methods and properties it has. You just need to call it according to the instructions of the document and it will be fine, which saves you time reading the code , also protects my copyright. For example, a method to obtain CPU information:
getCpuInfo(int cpuType);
This is an API. In other words, the usage method of the class described in this document is called API.
I can also develop a software to clean up junk files in my computer. I am more charitable and hope that more developers will use my software. I will accompany a document while releasing the software to tell you how Calling in your own program is also called API.
The Java API also has an explanation document, the entry address is: http://www.oracle.com/technetwork/java/api
Select the corresponding version of Java and click the link to enter. The API address of J2SE 1.7 is: http://docs.oracle.com/javase/7/docs/api/
This document is online and will be updated at any time. Of course, you can also download it locally. Please download it on Baidu by yourself.
Open the API document of J2SE 1.7, as shown in the figure below:
There are many packages in the Java class library:
Starting with java.* is the core package of Java, and all programs will use the classes in these packages;
The extension package begins with javax.*, and x means extension, which means extension. Although javax.* is an optimization and extension of java.*, since javax.* is used more and more, many programs rely on javax.*, so javax.* is also a part of the core and is also released with the JDK. .
Starting with org.* are packages published by various organizations or organizations. Because these organizations are influential and have high code quality, some of the commonly used classes they develop are also released with the JDK.
In terms of package naming, in order to prevent duplicate names, there is a convention: everyone starts with the reverse writing form of their own domain name to name the packages they develop. For example, the packages published by Baidu will start with com.baidu.* and are organized by w3c The published package will start with org.w3c.*, and the published packages by Weixueyuan will start with net.weixueyuan.*...
The domain name suffix of an organization is generally org, and the domain name suffix of a company is generally com. It can be considered that the packages starting with org.* are packages published by non-profit organizations. They are generally open source and can be used for free in their own products without using them. Consider infringement issues, and packages starting with com.* are often published by profitable companies, and there may be copyright issues. Be careful when using them.
Introduction to several commonly used packages in Java:
For more packages and instructions, please refer to the API documentation.
Search paths for Java import and Java classes <br />If you want to use classes in Java packages, you must first use the import statement to import.
The import statement is somewhat similar to #include in C language, with the syntax as:
import package1[.package2…].classname;
package is the package name and classname is the class name. For example:
import java.util.Date; // Import the Date class under the java.util package import java.util.Scanner; // Import the Scanner class under the java.util package import javax.swing.*; // Import the javax.swing package All classes under * represent all classes
Notice:
import can only import classes contained in a package, but not packages.
For convenience, we generally do not import separate classes, but import all classes under the package, such as import java.util.*;.
The Java compiler imports all classes in the JDK java.lang package by default to all Java programs (import java.lang.*;), which defines some commonly used classes, such as System, String, Object, Math, etc., so we These classes can be used directly without having to import explicitly. However, using other classes must be imported first.
The "Hello World" program mentioned earlier uses the System.out.println(); statement. The System class is located in the java.lang package. Although we did not explicitly import the classes in this package, the Java compiler has imported them for us by default. , otherwise the program will fail to execute.
Search path for Java classes
When a Java program runs, you need to import the corresponding class, that is, the process of loading the .class file.
Suppose there is the following import statement:
import p1.Test;
This statement indicates that the Test class in the p1 package is to be imported.
When installing JDK, we have set the environment variable CLASSPATH to indicate the path of the class library, its value is .;%JAVA_HOME%/lib, and JAVA_HOME is D:/Program Files/jdk1.7.0_71, so CLASSPATH is equivalent to .;D:/Program Files/jdk1.7.0_71/lib.
The Java runtime environment will search for and load the bytecode file Test.class in sequence to the following path:
.p1/Test.class ("." indicates the current path)
D:/Program Files/jdk1.7.0_71/lib/p1/Test.class
If the required class file is found under the first path, stop searching, otherwise continue searching for the subsequent paths. If the required class file is not found under all paths, an error occurs in compilation or running.
You can add a search path to the CLASSPATH variable, such as .;%JAVA_HOME%/lib;C:/javalib, then you can place the class file in the C:/javalib directory, and the Java running environment will also be found.