Java Coding Standard
To guide the development of Java Programs, and to ensure that each logical line (as
Purpose
documented in Java LOC Counting Standard) corresponds to one physical line.
Program Begin all programs with a descriptive header. The header should use the Java document
Headers commenting convention ("/**") so that automated documentation generation is possible
/**
* meanStd: calculates the mean and standard deviation of the numbers
* supplied on the command line.
Header example * @author Philip Johnson
* @version Tue Dec 26 11:03:18 1995
*/
Use descriptive names for all variables, function names, constants, and other identifie
Use single letter identifiers only for the counter in loops.
Follow the "standard Java" style as found in Sun's sources:
Identifiers Identifiers start with lower case.
Multi-word identifiers are internally capitalized.
Do not use hyphens or underscores to separate multi-word identifiers.
private static float sumDiffSquares = 0; //this is good
Identifier private static float x = 0; //this is bad
Example private static float sum_Diff_Squares = 0; //this is bad
Each class, method, and public variable should have a comment header
Implementation Comments. /*…*/. Implementation comments are means for
commenting out code or for comments about the particular implementation
Single-line comments //
Comments
Documentation Comments /**…*/. Doc comments are meant to describe the
specification of the code, from an implementation-free perspective to be read by
developers who might not necessarily have the source code at hand.
private static float sumDiffSquares = 0; //this is God
if (foo > 1) {
// Do a double-flip.
...
}
else{
return false; // Explain why here.
}
Comments
/*
Examples * This comment is ok
* Each line begins with *
*/
/*
This comment is wrong
Each line does not begins with *
*/
/* This comment is OK */
Blank Spaces Use one blank space to separate classes and methods.
Follow the "standard Java" style as found in Sun's sources:
Indent four spaces at a time.
Open braces (i.e. "{") do not start a new line.
Indenting
Close braces (i.e. "}") do start a new line, and are indented with the code they clo
if statements always use braces {}.
for (int i=0; i < [Link]; i = i + 1) {
[Link](new Float (args[i]), i);
}
if (condition) // this is bad! THIS OMITS THE BRACES {}!
statement;
if (condition) { // this is correct!
statement;
}
if (condition) {
statements;
}
else if (condition) {
statements;
}
Indenting else {
Example statements;
}
while (condition) {
statements;
}
try {
statements;
}
catch (ExceptionClass e) {
statements;
}
finally {
statements;
}
Follow the (regrettable) "standard Java" style as found in Sun's sources:
Classes and Interfaces begin with a capital letter.
Packages are all lower case.
Methods begin with a lower case letter.
Capitalization
Multi-word identifiers are internally capitalized in methods.
The names of variables declared class constants should be all uppercase with wo
separated by underscores (“_”).
Capitalization public class MeanStd
private static Vector vals = new Vector();
Examples
private static Vector diffSquares = new Vector();
package [Link]
static final int MAX_WIDTH = 999;
Each class should be contained in its own file.
Program
Each file should be named as the name of the class contained within it.
Modules
Packages are preferred.
Each line should contain at most one statement.
Statements
One declaration per line
argc++; // Correct
argv++; argc--; // this is bad!
Statements int level;
Examples int size;
[Link] = [Link] = 'c'; // this is bad!