Carefully touch the "documentation" 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. The matching characters can be imported into multiple class names.

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. Java. the applet contains some classes and interfaces for designing small applications (applets); Java. AWT is a window tool (awtabstract 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 public classes of some programs, such as datedictionary.

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 (rmiremote Method Invocation. In addition, the javax. package is an extension 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. Place the class generated by the Java file in the directory named directory

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

Create and use a package

// Filenameb. Java

Package com. Chen. Test // define a package

Public Class B // define a class

Public void addint iint J // sum two numbers and Output

System. Out. printlni + J

The above code is used to create a simple package: COM. Chen. Test. We name this file B. java. Now run javac-d c/B. Java to compile the package (this command generates the COM/Chen/test directory under drive C and saves the compilation result in B. class ). Now we can go to the C/COM/Chen/test directory to see that the compiled class file is also included.

OK. Now that this package has been created, you need to set C/COM/Chen/test in your environment variable classpath.

How to call a package

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

// Filenamea. Java

Import com. Chen. Test. // introduce the package just created

Public Class

Public static void mainstringargs

B test = new B

Test. add68 // use add to call a custom package

Save the above Java code as c/a. Java and compile it: javac c/a. java.

Tip: for the package file B. Java, you can just put it in the C/COM/Chen/test directory without compiling. 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 start with "/" and end with "/". They can contain common text, HTML tags, and javadoc tags. 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 <-- here we can embed an HTML sentence as an HTML comment -->

@ Version 0.1

/

Public class javadoctest

  

/

Use a string in main

@ See # mainjava. Lang. String

/

Static string sdisplay

/

Show javadoc Test

@ Param ARGs command line parameters

@ Return no return value

/

Public static void mainstring ARGs

  

Sdisplay = "javadoc test ″

System. Out. printlnsdisplay

In this Java program, a large number of javadoc, text, and HTML tags are used. The javadoc Mark starts with "/", indicating that these are Java code annotations. 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; another type of tag is inline tag. This tag is in the form of @ tag, which allows developers to create hyperlinks to link from the comments part 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, and <--> are HTML annotations.

@ 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 the method of the same class, use: @ link # getcomponentatintgetcomponentat

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

Related Article

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.