Java language coding specification (1)

zhaozj2021-02-17  54

If you need to copy, spread, please attach this statement, thank you. Original source: http://java.sun.com/docs/codeconv/html/codeconvtoc.doc.html, the translation source: http://morningspace.51.net/ ,moyingzz@etang.com

1 Introduction

1.1 Why have the code specification (Why Have Code Conventions)

The code specification is especially important for programmers. There are several reasons: - A software cycle of software is maintenance - there is almost no software, in its entire life cycle, all of the initial developers To maintain - encoding specifications can improve software readability, allowing programmers to completely understand new code as soon as possible - if you put the source code as a product, you need to make it very well packaged and clear, one If any other products you have built In order to implement specifications, each software developers must consistently comply with the coding specification. everyone.

1.2 Copyright Notice (ACKNOWLEDGMENTS)

This document reflects the coding standard part of Sun Microsystems, Java language norms. The main contributors include: Peter King, Patrick Naughton, Mike Demoney, Jonni Kanerva, Kathy Walrath, and Scott Hommel. This document is now maintained by Scott Hommel, please send it to Shommel@eng.sun.com for comments.

2 file name (File names)

This part lists the commonly used file names and their suffixes.

2.1 File suffixes (File Suffixes)

Java program uses the following file suffix:

File Category File suffix java source file .javajava byte code file .class

2.2 Common file names (Common file names)

Common file names include:

The preferred file name of the file name use gnumakefilemakefiles. We use Gnumake to create a (Build) software. ReadMe Overview of the preferred file name of the file included in the specific directory

3 file organization (File ORGANIZATION)

A file is constructed by a paragraph divided by a space, and an optional annotation that identifies each paragraph. More than 2,000 rows of procedures are difficult to read and should be avoided as much as possible. "Java Source File Example" provides a reasonable Java program example.

3.1 Java Source File (Java Source Files)

Each Java source file contains a single public class or interface. If private and interfaces are associated with a public class, they can put them and public classes in the same source file. The public category must be the first class or interface in this file. The Java source file also follows the following rules:

- At the beginning of the comment (see "Opening Note") - Package and introduction statement (see "Package and Introduction Statements") - Class and Interface Declaration (see "Class and Interface Declaration")

3.1.1 BEGINNING Comments

All source files should have a C language style annotation in the beginning, where the class name, version information, date, and copyright statement: / *

* ClassName

*

* Version Information

*

* Date

*

* CopyRight Notice

* /

3.1.2 Packages and introduction statements (Package and Import Statements)

In most Java source files, the first non-promised line is a packet statement. You can follow the introduction statement after it. For example: package java.aw;

Import java.awt.peer.canvaspeer; 3.1.3 and interface declaration (Class and Interface Decam)

The following table describes the various parts of the class and interface declarations and the order of them appear. See "Java Source File Example" An example of a comment.

Particular / Interface Declaration of Part 1 / Interface Document Note (/ ** ... * /) This annotation is included in this comment, see "Document Notes" 2 or Interface Declaration 3 / Interface Implementation (/ * ... * /) If necessary, the comment should contain any information about the entire class or interface, and this information is not suitable as a class / interface document comment. The 4 types of (static) variables are firstcoming the common variables of the class, followed by the protection variable, and then the package level variable (no access modifier, access modifier), and finally the private variable. 5 instance variables are first common, followed by the protection level, and then the package level (no access to the modifier), and finally the private level. 6 Structure 7 Methods These methods should be packet according to functions, rather than scope or access. For example, a private class method can be placed between two public instance methods. Its purpose is to make it easier to read and understand the code.

4 indentation typesetting (Indentation)

4 spaces are often used as a unit of indentation. The exact interpretation of indentation is not specified in detail (space VS. Table). A tab is equal to 8 spacers (rather than 4).

4.1 Line Length

Try to avoid a row of more than 80 characters, because many terminals and tools cannot be well processed. Note: The examples used in the document should use a shorter chance, and the length is generally not more than 70 characters.

4.2 Wrapping Lines

When an expression cannot be accommodated in one line, it can be disconnected according to the following general rules: - Disconnect behind a comma - disconnected in front of an operator - Ning better to select a higher level (Higher-Level), Not a lower level (Lower-Level) - New line should be aligned with the beginning of the same level expression - If the above rules have caused your code to confuse or make your code piled up on the right, then In order to indent the 8 spaces. The following is some examples of disconnect calls: SomeMethod (LONGEXPRESSION1, LONGEXPIPRESSION2, LONGEXPRESSION3,

LONGEXPIPRESSION4, LONGEXPRESSION5);

Var = SomeMethod1 (LONGEXPRESSION1,

SomeMethod2 (LONGEXPRESSION2,

LONGEXPIESSION3)))

The following is an example of two breakdown spell expressions. The former is better because the disconnect is located outside the bracket expression, which is a higher level of disconnection. Longname1 = longname2 * (longname3 longname4 - longname5)

4 * longname6; // preffer

Longname1 = longname2 * (longname3 longname4

- longname5) 4 * longname6; // Avoid

The following is an example of two indentation methods declared. The former is a conventional situation. If the latter uses a routine indentation method, the second line and the third row are moved very rely very, so the generation is indented in 8 space // conventional Indentation.

SomeMethod (int Anarg, Object Anothererarg, String Yetanotherarg,

Object andstillanother {

...

}

// Indent 8 spaces to avoid very deep indents

Private static synchronized horkinglongmethodname (int Anarg, Object Anothererarg, String Yetanothererarg,

Object andstillanother {

...

}

The wrap of the IF statement usually uses 8 space rules, because conventional indentation (4 spaces) will make the statement look more difficult. For example: // don't use this indentation

IF (Condition1 && Condition2)

|| (Condition3 && Condition4)

||! (Condition5 && condition6)) {// bad wraps

DOSMETHINGABOUTIT (); // make this line easy to miss

}

// use this indeetation instead

IF (Condition1 && Condition2)

|| (Condition3 && Condition4)

||! (Condition5 && condition6)) {

DOSMETHINGABOUTIT ();

}

// or us

IF (Condition1 && Condition2) || (Condition3 && Condition4)

||! (Condition5 && condition6)) {

DOSMETHINGABOUTIT ();

}

There are three feasible methods here for processing three-yuan arithmetic expression: alpha = (AlongBooleaneXpression)? Beta: gamma;

Alpha = (AlongBooleaneXpression)? Beta

: gamma;

Alpha = (AlongBooleaneXpression)

? beta

: gamma;

转载请注明原文地址:https://www.9cbs.com/read-29156.html

New Post(0)