Java Reference
In-Depth Information
To see an example of a javadoc HTML file, check out
www.cs.armstrong.edu/liang/javadoc/
1.9.2 Proper Indentation and Spacing
A consistent indentation style makes programs clear and easy to read, debug, and maintain.
Indentation
is used to illustrate the structural relationships between a program's components
or statements. Java can read the program even if all of the statements are on the same long
line, but humans find it easier to read and maintain code that is aligned properly. Indent each
subcomponent or statement at least
two
spaces more than the construct within which it is
nested.
A single space should be added on both sides of a binary operator, as shown in the follow-
ing statement:
indent code
System.out.println(
3
+
4
*
4
);
Bad style
System.out.println(
3
+
4
*
4
);
Good style
1.9.3 Block Styles
A
block
is a group of statements surrounded by braces. There are two popular styles,
next-line
style and
end-of-line
style, as shown below.
public class
Test
{
public class
Test {
public static void
main(String[] args) {
System.out.println(
"Block Styles"
);
}
}
public static void
main(String[] args)
{
System.out.println(
"Block Styles"
);
}
}
Next-line style
End-of-line style
The next-line style aligns braces vertically and makes programs easy to read, whereas the
end-of-line style saves space and may help avoid some subtle programming errors. Both are
acceptable block styles. The choice depends on personal or organizational preference. You
should use a block style consistently—mixing styles is not recommended. This topic uses the
end-of-line
style to be consistent with the Java API source code.
1.41
✓
✓
Reformat the following program according to the programming style and documen-
tation guidelines. Use the end-of-line brace style.
Check
Point
public class
Test
{
// Main method
public static void
main(String[] args) {
/** Display output */
System.out.println(
"Welcome to Java"
);
}
}
Search WWH ::
Custom Search