The concept of Java class Library and the usage method of import are _java

Source: Internet
Author: User
Tags documentation

Java class Library and its organizational structure (Java API)

The Java authorities provide developers with a number of powerful classes that are placed in separate packages and published with the JDK, called Java class libraries or Java APIs.

API (Application Programming Interface, Application programming Interface) is a common concept.

For example, I wrote a class to get a variety of computer hardware information, it is very strong and stable, if your project also needs such a function, then you do not have to write your own code, you can use my class directly. However, my class code is very complex, let you read the code is not realistic, and I do not want you to see my code (you do not need to read these obscure code), I want to protect my copyright, how to do?

I can compile my class first with a document that tells you how my class is used, what methods and attributes you need to call it, just follow the instructions in the document, save the time you read the code, and protect my copyright. For example, how to get CPU information:

  Getcpuinfo (int cputype);


This is an API. That is, the use of the class described in the document is called an API.

I can also develop a software to clean up the garbage files in the computer, I have a good sense of charity, I hope that more developers to use my software, I will release the software along with a description of the document to tell you how to call in your own program, which is also called the API.

The Java API also has a description document, entry address: Http://www.oracle.com/technetwork/java/api

Select the corresponding version of Java, click on the link to enter. The API address for J2SE 1.7 is: http://docs.oracle.com/javase/7/docs/api/

This document is online and will be updated by the authorities at any time. Of course, you can also download to the local, please how to download their own Baidu.

Open the API documentation for J2SE 1.7, as shown in the following illustration:

There are many packages in the Java class Library:
Starting with Java.* is the Java core package, which all programs use in the classes in these packages;
Starting with javax.* is the expansion package, X is the meaning of extension, that is, extension. Although javax.* is an optimization and extension of java.*, many programs rely on javax.* because of the increasing use of javax.*, so javax.* is also a part of the core and is also published with JDK.
Starting with org.* are packages published by various agencies or organizations, because they are influential, their code is of high quality, and so are some of the commonly used classes they develop with the JDK.

In terms of the naming of packages, in order to prevent duplicate names, there is a convention: everyone to their own domain name in the inverted form as the beginning of their own development of the package named, such as Baidu's release of the package will start with com.baidu.*, the package published by the consortium will start with Org.w3c.*, the release of micro-court package will be Net.weixueyuan.* the beginning of ...

An organization's domain name suffix is generally org, the company's domain name suffix is generally com, can be considered as the beginning of org.* package for non-profit organizations issued packages, they are generally open source, can be used free of charge in their own products, not to consider infringement, and the beginning of com.* package is often made by for-profit companies to publish , you may have a copyright issue that you should pay attention to when you use it.

Introduction to several commonly used packages in Java:

Please refer to the API documentation for more packages and instructions.

Java Import and search paths for Java classes
If you want to use a class in a Java package, you must first import using the import statement.

The import statement is similar to the #include in the C language, and the syntax is:

  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 import Java.util.Scanner under the Java.util package
;//Import the Scanner class import javax.swing.* under the Java.util package
;//import JAV All classes under the Ax.swing package, * representing all classes


Attention:
Import can import only the classes that the package contains, not the packages.
For convenience, we generally do not import separate classes, but instead import all the classes under the package, such as the import java.util.*;

The Java compiler defaults to importing all of the classes in the JDK's Java.lang package (import java.lang.*) for all Java programs, which define common classes such as System, String, Object, Math, and so on. So we can use these classes directly instead of explicitly importing them. However, the use of other classes must be imported first.

The "Hello World" program mentioned earlier uses SYSTEM.OUT.PRINTLN (); Statement, the System class is located in the Java.lang package, although we did not explicitly import the classes in the package, but the Java compiler has already imported the default for us, otherwise the program fails.
Search path for Java classes

The Java program runs to import the corresponding class, which is the process of loading the. class file.

Suppose the following import statement:

Import P1. Test;


The statement indicates that you want to import the Test class in the P1 package.

When installing the JDK, we have set the environment variable CLASSPATH to indicate the path of the class library, whose 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 will look for and load the bytecode file Test.class in turn to the following path:
. P1\test.class ("." Represents the current path)

D:\Program Files\jdk1.7.0_71\lib\p1\test.class

If you find the required class file under the first path, stop the search, or continue to search for the following path, and if you cannot find the required class file under all the paths, compile or run an error.

You can add a search path to the CLASSPATH variable, such as.; %java_home%\lib; C:\javalib, then you can put the class file in the C:\javalib directory, the Java run environment will be found.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.