To calculate the next value in the sequence we perform some simple arithmetic and again use the = operator
to assign the value of the arithmetic expression on the right to the variable on the left. As you would expect,
the + operator calculates the sum of its operands, and the - operator calculates the difference. The language
defines a number of arithmetic operators for the primitive integer and floating-point types including addition
(+), subtraction (-), multiplication (*), and division (/), as well as some other operators we talk about later.
Notice that the println method accepts an integer argument in the Fibonacci example, whereas it
accepted a string argument in the HelloWorld example. The println method is one of many methods
that are overloaded so that they can accept arguments of different types. The runtime system decides which
method to actually invoke based on the number and types of arguments you pass to it. This is a very powerful
tool.
Exercise 1.3: Add a title to the printed list of the Fibonacci program.
Exercise 1.4: Write a program that generates a different sequence, such as a table of squares.
1.3. Comments in Code
The English text scattered through the code is in comments. There are three styles of comments, all illustrated
in the Fibonacci example. Comments enable you to write descriptive text alongside your code, annotating
it for programmers who may read your code in the future. That 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.
Text that occurs between / and / is ignored by the compiler. This style of comment can be used on part of
a line, a whole line, or more commonly (as in the example) to define a multiline comment. For single line and
part line comments you can use // which tells the compiler to ignore everything after it on that line.
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 previous example is for the main method. These
comments can be extracted by a tool that uses them to generate reference documentation for your classes. By
convention, lines within a documentation comment or a /.../ comment have a leading asterisk (which is
ignored by documentation tools), which gives a visual clue to readers of the extent of the comment.
1.4. Named Constants
Constants are values like 12 , 17.9, and "StringsLikeThis". Constants, or literals as they are also
known, are the way you specify values that are not computed and recomputed but remain, well, constant for
the life of a program.
The Fibonacci example printed all Fibonacci numbers with a value less than 50. The constant 50 was used
within the expression of the while loop and within the documentation comment describing main. Suppose
that you now want to modify the example to print the Fibonacci numbers with values less than 100. You have
to go through the source code and locate and modify all occurrences of the constant 50. Though this is trivial
in our example, in general it is a tedious and error-prone process. Further, if people reading the code see an
expression like hi< 50 they may have no idea what the constant 50 actually represents. Such "magic
numbers" hinder program understandability and maintainability.
A named constant is a constant value that is referred to by a name. For example, we may choose the name
MAX to refer to the constant 50 in the Fibonacci example. You define named constants by declaring fields of
the appropriate type, initialized to the appropriate value. That itself does not define a constant, but a field