How to use Java API
What is a Java class library
When writing programs, there are usually many functions that are general or very basic. These functions can be used to form more complex function code. For example, file operation, the operation of different programs on files is basically the same, opening files, closing files, reading data in files, writing data to files, and so on. The only difference is that the file paths are different and the file contents are different. If the file related operations are written into a general class, no matter which programmer can use it directly, without having to rewrite all the codes of the operation file, the programmer's work efficiency will be greatly improved. In this way, some general functions are written into corresponding class code to form a class library.
The Java class library is a collection of implemented standard classes provided by the Java language, Is an API for Java programming (application program interface), which can help developers develop java programs conveniently and quickly. When developing programs, programmers can directly call these ready-made classes. These classes can be divided into different collections according to different functions. Each collection forms a package called class library. Most of the Java class libraries are provided by Sun company , these class libraries are called basic class libraries.
What is a package
A package is a concept used to organize classes. We can use the concept of "administrative division" to help understand the package. A country is divided into many provinces and many cities in the province. We can continue to subdivide more administrative units. These administrative units define an area and all people and things in the area. The package is equivalent to the administrative division, and the people and things in the administrative division are equivalent to the classes in the package. Therefore, administrative division is an abstract concept. Package, like administrative division, is an abstract concept. Packages can also contain sub packages. For example: com myJava. Utils such a package name, just like Guangdong Province Guangzhou Tianhe District.
Note:
(1) Package cannot be preceded by spaces. The package declaration should be on the first line of the source file. Each source file can only have one package declaration (that is, there is only one package declaration statement). Each type in this file belongs to the package.
(2) Package names are generally all lowercase. Java requires that packages have domain name prefixes to distinguish different authors and reverse write domain names, such as com google. guava. guava-parent。
(3) Package naming is usually associated with the where code files are stored. As mentioned above The path to save the java file is net / Java / util / something java。 Because something The package name declared in the java file is net java. util
(4) If a package declaration is not used in a source file, the class will be considered by the compiler to be placed in a default package without a name. Although the default package is good for short example programs, it is not appropriate for practical applications. In most cases, you need to define a package for your code.
Using java class libraries
The way to use the Java class library (or Java API) is to use the import statement, that is, in the Java source file, the import statement should be after the package statement and before the definition of all classes. There can be no or multiple import packages. The import package has two syntax forms:
Single type import, such as import Java io. File;
Type import on demand, such as import Java io.*;
For on-demand type import (import Java. Io. *;), Some people misunderstand it as importing all classes under a package. In fact, it is not. You can see from the name that it will only import on demand, that is, it does not import the whole package, but only the classes that need to be used by the current class.
Are the above two methods of importing packages the same? dissimilarity! Single type import and on-demand type import have different positioning algorithms for class files. Please find relevant materials for specific instructions.
You can see from the source code of JDK that sun software engineers generally do not use on-demand import. Because using single type import has at least the following two benefits:
1。 Improve compilation speed.
2。 Avoid naming conflicts. (for example, when you import Java. AWT. *; import Java. Util. *, the compiler will make a compilation error when using list)
Of course, using single type import will make your import statement look very long.
Java class library online Chinese document reference: http://tool.oschina.net/apidocs/apidoc?api=jdk -zh
Practical training
Step 1: create a small game project named MySee.
Step 2: add a game window class in the web project, and the class name is drawsee. In drawsee Java file header, add the following package:
The above mainly uses Java AWT package and javax Swing package.
java. awt:java. AWT is a software package, which was used to write graphical interface applications in the early days. Such as color class and font class.
javax. Swing: a newly developed graphical interface package to solve the problems existing in AWT. Swing is an improvement and extension of AWT.
Step 3: in drawsee Java file, add the code of window configuration:
The code of window configuration is usually placed in the initialization method, so that some properties of the window are specified when instantiating the object, and then the window will be displayed according to this setting.
Step 4: add an entry method to the project. Add a new class in MySee project: appmain class. The class code is as follows:
Step 5: run the program to see a window with blank content.
summary
The above is all about the detailed explanation of the use of Java API in this article. I hope it will be helpful to you. Interested friends can continue to refer to this site: detailed explanation of producer consumer problems and reader writer problems in Java, detailed explanation of list in Java collection, detailed explanation of producer consumer problems in BlockingQueue of Java Concurrent learning, etc. you can leave a message at any time, Xiaobian will reply to you in time. Thank you for your support!