Touch "document" and "package" in Java programming"

Source: Internet
Author: User
Tags html comment

What is a package

A Package in Java actually refers to a directory, which is used to better manage Java classes and interfaces ). Java packages can be used by another Java Development Kit. If we want to reference the class in a package, use the import keyword to mark it. For example:

Import java. util .??

Date = new Date?

??

Tip: import java. util .?? Indicates that all public classes and interfaces in java. util are introduced to the current package. Here? You can call multiple class names in a match.

Common Java standard packages

JDK provides many standard Java classes and interfaces. These packages are required to write Java programs and understand the classes and interfaces contained in each package, being familiar with these classes and interfaces is a basic skill that every Java programmer should master.

Common Java packages include Java. applet, java. awt, java. I, java. lang, java.net, and java. util. Among them, java. applet contains some classes and interfaces for designing small applications (applets); java. awt is a window tool luggage (awt? Abstract Window Toolkit), which contains some GUI-related classes; java. io packages support input and output, such as file input stream class: FileInputStream and so on; java. lang contains threads, exceptions, systems, integers, and other related classes. It is a default package loaded in Java programs. java.net supports the TCP/IP network protocol, and contains Socket and URL-related classes, which must be used in network programming; java. util contains some program public classes, such as Date? Dictionary and so on.

In addition to some standard packages mentioned above, there are many other packages. For example, we may need to use java for database programming. SQL package. java is also used to write network programs. rmi package (RMI? Remote Method Invocation. In addition, Javax .? Packages are extensions of some standard packages. Common packages include javax. swing and javax. sound.

Create a package by yourself

Java packages can be created by yourself. We can place classes and interfaces with similar functions in the same package to facilitate management and use.

How to Create a package

1. Define the Public class;

2. Add "Package name" in the first sentence ";

3. Put the Class generated by the Java file in the directory named?

4. Use "import package name" in other programs to access all Public classes in this package.

Create and use a package

// Filename? B. java

Package com. chen. test? // Define a package

Public class B? // Define a class

Public void add? Int I? Int j ?? // Sum two numbers and Output

System. out. println? I + j ??

The above code is used to create a simple package: com. chen. test. We name this file B. java. Run javac-d C now? B. Compile the package in java (this command generates the comchenest directory under drive C and stores the compilation result in B. class ). Now let's go to C? The comchenest directory shows that the compiled Class file is also included.

OK. Now this package has been created. What you need to do is? Comchenest is set in your environment variable classpath.

How to call a package

Now we use a small program to call the package created above:

// Filename? A. java

Import com. chen. test .?? // Introduce the package just created

Public class?

Public static void main? String ?? Args ??

B test = new B ???

Test. add? 6? 8 ?? // Use add ?? Call a custom package

Save the above Java code as C? A. java? Then compile: javac C? A. java.

Tip: Can you skip compiling for package file B. java? Instead, just put it in C? In the comchenest directory. Note that the files A. java and B. java cannot be stored in the same directory, and errors may occur.

Java documentation and Javadoc

In addition to packages, Java documentation is also a very important concept in Java programming. Java documentation is the help documentation for Java programs. In general, we should first write the help document and use the tool Javadoc to generate the corresponding document. Javadoc is a document generation tool provided by JDK. It uses the Java Compilation Program Javac to perform syntax analysis on the declarations and document annotations in the program code source file, by default, a set of HTML documents are generated to describe classes, internal classes, interfaces, constructors, methods, and fields. Javadoc comments with "/?" Start with "? /", Which can contain common text, HTML tag, and Javadoc tag. Javadoc only processes comments in the source file before class/interface definitions, methods, fields, and constructors, and ignores comments from other places.

Understanding Javadoc

Now we use a routine to illustrate the significance of Javadoc marking:

/??

? My JavaDoc test program -- JavaDocTest

? @ Author warton2003 Xi'an

? Test

? @ Version 0.1

? /

Public class JavaDocTest

?

/??

? In main ?? To use strings

? @ See # main? Java. lang. String ???

? /

Static String SDisplay?

/??

? Show Javadoc Test

? @ Param args command line parameters

? @ Return no return value

? /

Public static void main? String args ???

?

SDisplay = "Javadoc test ″?

System. out. println? SDisplay ??

In this Java program, a large number of Javadoc, text, and HTML tags are used. Javadoc mark as "/?" It indicates that these are Java code comments. The Mark starting with "@" is the Javadoc mark.

The Javadoc tag has two types: Standalone tag and Inline tag. Independent tags are common tags, such as @ param, @ return, and @ author. Is an inline tag of another type? Is this flag used? @ Tag? ? It allows developers to create hyperlinks to link from the comments to other Javadoc pages or other places on this page.

Tip: Correct Use of Javadoc mark in Java source code is a very necessary annotation habit, which will help Javadoc automatically generate a complete format API document from the source code file.


OK. Let's take a look at how Javadoc generates beautiful documents! Save the above Code to JavaDocTest. java and compile it.

Run: javadoc-private-d doc-auth

Or-version JavaDocTest. java

At this time, the Javadoc program will generate a doc directory based on JavaDocTest. java, which contains many HTML files. These are the API help documentation generated by Javadoc.

Javadoc command line syntax

You can only use the command line of the Javadoc. Finally, we will introduce the simple Javadoc command.

The command line Syntax of Javadoc is as follows:

Javadoc options packagenames sourcefiles @ files

?

Tip: for general applications, we only need to use command lines such as javadoc yourjavafile. java to generate the help document. You can use the javadoc help command to view the help documentation of Javadoc.

In short, Javadoc provides a complete and standardized API documentation function. In software project management and development, the rational use of Javadoc can not only reduce the document workload during development, improve efficiency, but also facilitate future software modification and maintenance.

Some detailed descriptions of Javadoc mark:

@ Author specifies the "author" item in the generated document. You can specify multiple author items. In the code on the left, the author is followed by an HTML tag to link URLs. All are HTML tags, It is an HTML comment.

@ Version specifies the version information.

@ See tags are complex. @ See "string" adds text items for "string" without any links. @ See Label uses HTML tags to generate links. @ See package. class # member Label.

@ Param mark is used to describe parameters.

@ Return indicates the return value.

@ Link indicates the inline tag. The common format of the inline tag is :? @ Linkpackage. class # member label?

Tip: Label is the text in the middle of the comment. package. class # member points to a specific class or method. Here are some examples:

Link to the attributes of the same class and use @ link # KEY.

Link to a method of the same class, use :? @ Link # getComponentAt? Int? GetComponentAt

Link to other classes and use @ linkjava. util. Map

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.