Previous | Table of Contents | Next |
Exercise 13.3
Add a title to the printed list.
Exercise 13.4
Write a program that generates a different sequence, such as a table of squares (multiplication is done using *, such as i * i).
The English-like things scattered through the code are comments. Java has three styles of comments, all illustrated in the example.
Text following // up to the end of the line is ignored by the compiler, as is text between /* and the next */.
Comments enable you to write descriptive text beside your code, to annotate it for future programmers who may read your code in the future. The future programmer may well be you months or years later. You save yourself effort by commenting your own code. Also, you often find bugs when you write comments: because explaining what the code is supposed to do forces you to think about it.
The third kind of comment appears at the very top, between /** and */. A comment starting with two asterisks is a documentation comment (doc comment for short). Documentation comments are intended to describe declarations that follow them. The comment in the above example is for the main method. A tool called javadoc extracts documentation comments and generates HTML documentation.
Constants are values like 12, 17.9, and Strings Like This. Constants are how you specify values that are not computed and recomputed, but remain, well, constant for the life of a program.
Programmers prefer named constants for two reasons. One reason is that the name of the constant is a form of documentation. The name can (and should) describe what the particular value is used for.
Another reason is that you define a named constant in a single place in a program. When the constant needs to be changed or corrected, it can be changed in only one place, easing program maintenance. Named constants in Java are created by declaring a variable as static and final, and providing its initial value in the declaration:
class CircleStuff { static final double π = 3.1416; }
The value of π can be changed in just one place when we discover that five significant digits of precision are not enough. We declared π as doublea double-precision 64-bit floating-point number. Now we could change π to a more precise value, like 3.14159265358979323846.
You can group related constants within a class. For example, a card game might use these constants:
class Suit { final static int CLUBS = 1; final static int DIAMONDS = 2; final static int HEARTS = 3; final static int SPADES = 4; };
With this declaration, Suit in a program would be accessed as Suit.HEARTS, Suit.SPADES, and so on, thus grouping all the suit names within the single name Suit.
We take a minor diversion to note the π symbol as the name of a constant in the previous example. In most programming languages, identifiers are usually limited to the letters and digits available in the ASCII character set.
Java moves you toward the modern world of internationalized software: You write Java code in Unicodean international character-set standard. Unicode characters are 16 bits and provide a character range large enough to write the major languages used in the world, which is why we can use π for the name of the value in the example above. π is a valid letter from the Greek section of Unicode, and therefore valid in Java source. Most existing Java code is written in ASCII, a 7-bit character standard, or ISO-Latin-1, an 8-bit character standard commonly called Latin-1. But, they are translated into Unicode before processing so the Java character set is always Unicode.
Exercise 13.5
Change the HelloWorld application to use a named string constant as the string to print.
Exercise 13.6
Change the Fibonacci application to use a named constant in its loop instead of a literal constant.
Previous | Table of Contents | Next |