没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Java
Code Conventions
September 12, 1997ii
Please
Recycle
Copyright Information
1997, SunMicrosystems, Inc. All rights reserved.
2550 Garcia Avenue,Mountain View, California 94043-1100 U.S.A.
This document is protected by copyright.No part of this documentmay be reproduced in any
formby anymeans
without priorwritten authorization of Sun and its licensors, if any.
The information described in this documentmay be protected by one ormore U.S. patents, foreign
patents, or
pending applications.
TRADEMARKS
Sun, SunMicrosystems, SunMicroelectronics, the Sun Logo, SunXTL, JavaSoft, JavaOS, the
JavaSoft Logo, Java,
HotJava Views,HotJJavaChips, picoJava,microJava, UltraJava, JDBC, the Java Cup and
SteamLogo, “WriteOnce,
Run Anywhere” and Solaris are trademarks or registered trademarks of SunMicrosystems, Inc. in
the United States
and other countries.
UNIX® is a registered trademark in the United States and other countries, exclusively licensed
through X/Open
Company, Ltd.
Adobe ® is a registered trademark of Adobe Systems, Inc.
NetscapeNavigator™ is a trademark ofNetscape Communications Corporation.
All other product namesmentioned herein are the trademarks of their respective owners.
THIS DOCUMENT IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESS OR
IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
THIS DOCUMENT COULD INCLUDE TECHNICAL INACCURACIES OR
TYPOGRAPHICAL ERRORS.
CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE
CHANGES WILL BE
INCORPORATED IN NEW EDITIONS OF THE DOCUMENT. SUN MICROSYSTEMS, INC.
MAY MAKE
IMPROVEMENTSAND/OR CHANGES INTHE PRODUCT(S)AND/OR THE
PROGRAM(S)DESCRIBEDINTHIS
DOCUMENT AT ANY TIME.June 2, 1997
iii
1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Why Have Code Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 File Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.1 File Suffixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Common File Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 File Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.1 Java Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.1.1 Beginning Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.1.2 Package and Import Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.1.3 Class and Interface Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.1 Line Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.2 Wrapping Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.1 Implementation Comment Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.1.1 Block Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.1.2 Single-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.1.3 Trailing Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.1.4 End-Of-Line Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.2 Documentation Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
6 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
6.1 Number Per Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
6.2 Placement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
6.3 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6.4 Class and Interface Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
7 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
7.1 Simple Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
7.2 Compound Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7.3 return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7.4 if, if-else, if-else-if-else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7.5 for Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.6 while Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.7 do-while Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.8 switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.9 try-catch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
8 White Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
8.1 Blank Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
8.2 Blank Spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9 Naming Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
10 Programming Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
10.1 Providing Access to Instance and Class Variables . . . . . . . . . . . . . . . . . . . 15June 2, 1997
iv
10.2 Referring to Class Variables and Methods . . . . . . . . . . . . . . . . . . . . . . . . . 16
10.3 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
10.4 Variable Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
10.5 Miscellaneous Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
10.5.1 Parentheses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
10.5.2 Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
10.5.3 Expressions before ‘?’ in the Conditional Operator. . . . . . . . . . . 17
10.5.4 Special Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
11 Code Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
11.1 Java Source File Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 - File Names
1
Java Code Conventions
1 - Introduction
1.1 Why Have Code Conventions
Code conventions are important to programmers for a number of reasons:
• 80% of the lifetime cost of a piece of software goes to maintenance.
• Hardly any software is maintained for its whole life by the original author.
• Code conventions improve the readability of the software, allowing engineers to
understand new code more quickly and thoroughly.
• If you ship your source code as a product, you need to make sure it is as well packaged
and clean as any other product you create.
1.2 Acknowledgments
This document reflects the Java language coding standards presented in the Java Language
Specification, from Sun Microsystems. Major contributions are from Peter King, Patrick
Naughton, Mike DeMoney, Jonni Kanerva, Kathy Walrath, and Scott Hommel.
For questions concerning adaptation, modification, or redistribution of this document, please
read our copyright notice at http://java.sun.com/docs/codeconv/html/Copyright.doc.html.
Comments on this document should be submitted to our feedback form at http://java.sun.com/
docs/forms/sendusmail.html.
2 - File Names
This section lists commonly used file suffixes and names.2 - File Names
23 - File Organization
3
2.1 File Suffixes
JavaSoft uses the following file suffixes:
2.2 Common File Names
Frequently used file names include:
3 - File Organization
A file consists of sections that should be separated by blank lines and an optional comment
identifying each section.
Files longer than 2000 lines are cumbersome and should be avoided.
For an example of a Java program properly formatted, see “Java Source File Example” on page
19.
3.1 Java Source Files
Each Java source file contains a single public class or interface. When private classes and
interfaces are associated with a public class, you can put them in the same source file as the
public class. The public class should be the first class or interface in the file.
Java source files have the following ordering:
• Beginning comments (see “Beginning Comments” on page 4)
• Package and Import statements; for example:
import java.applet.Applet;
import java.awt.*;
import java.net.*;
• Class and interface declarations (see “Class and Interface Declarations” on page 4)
File Type Suffix
Java source .java
Java bytecode .class
File Name Use
GNUmakefile The preferred name for makefiles.
We use gnumake to build our software.
README The preferred name for the file that summarizes the
contents of a particular directory.3 - File Organization
4
3.1.1 Beginning Comments
All source files should begin with a c-style comment that lists the programmer(s), the date, a
copyright notice, and also a brief description of the purpose of the program. For example:
/*
* Classname
*
* Version info
*
* Copyright notice
*/
3.1.2 Package and Import Statements
The first non-comment line of most Java source files is a package statement. After that,
import statements can follow. For example:
package java.awt;
import java.awt.peer.CanvasPeer;
3.1.3 Class and Interface Declarations
The following table describes the parts of a class or interface declaration, in the order that they
should appear. See “Java Source File Example” on page 19 for an example that includes
comments.
Part of Class/Interface
Declaration Notes
1 Class/interface documentation
comment (/**...*/)
See “Documentation Comments” on page 9 for
information on what should be in this comment.
2 class or interface statement
3 Class/interface implementation
剩余17页未读,继续阅读
资源评论
- zhanglei8455672013-06-15适合想提高编程者
jiayeshi
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功