1.10 Constants

In our programs so far, we have seen a variety of constant values. Some of the constants that we have used were numeric, either integers or floating point values:

-453, 4.8E3, -87.102, 9876

Others were char constants, characters surrounded by apostrophes:

'!', '\\', '4'
Still others were the two boolean constants:

true, false
Constants (of any type) like those shown here, are often referred to as literal constants or simply literals. A literal, roughly speaking, is something that represents itself. Java can deduce a literal's type and value by examining the characters used to write the literal.

Java also allows us to associate an identifier with a constant value through the use of the final modifier in the declaration of a variable.

Example 1

The statements that follow associate the identifier CLASS_SIZE with the int value 30 and TERMINATOR with the >char value'*'.
final int CLASS_SIZE = 30;
final char TERMINATOR = '*'; 

Notice that the identifiers used here are made up of upper case letters with, possibly, the underscore for clarification. This is not required by Java but it is a common convention, one that you should follow.

Once an identifier has been declared to be final, its value can never be changed at any point in the program. An attempt to do so will produce an error message. It can, however, be used anywhere that a literal value of that type could be used.

Why would we want to have identifiers associated with constants?

Why use named constants instead of literals throughout our programs?

For one thing, named constants make programs easier to understand. If someone else were to read one of your programs (or if you were to read it some time after you had written it), it would likely be much easier to understand if it used a name like VOTING_AGE rather than the literal value 18.

Another reason is that the use of named constants can often make revisions to programs easier. Suppose, for example, that we have written a fairly large program to keep various records of the progress of the students in a computer science class. Suppose further that the program was originally written for a class of 30 students but is now to be revised to accommodate a class of 40 students. If we had used literals in our program, we might have to change 30 to 40 in many places in the program. If we had used a named constant, we would only have to make one change - in the declaration of the constant.

A third reason for their use is to help programmers avoid errors. Suppose again that we are revising our class record program to accommodate a larger class. If we have used literals and we change every occurrence of 30 to 40, we might not just increase the class size to 40 but also make some changes that we did not want. We might, for example, inadvertently change the percentage allocated to term work from 30% to 40%.

Literal constants in programs are often referred to as magic numbers. Just as we are left wondering how a magician makes a rabbit appear out of nowhere, we might be equally in the dark as to why a variable was assigned the value 12. Generally speaking, one should try to avoid magic numbers. If you are not convinced by the arguments against them that we have given here, perhaps the fact that many instructors take marks off programs that contain magic numbers will bring you over to our point of view.

As well as allowing us to create our own named constants, Java also provides us with some pre-defined named constants. Two of these are important mathematical constants; pi, a value that shows up in almost every branch of mathematics, and e, a constant that appears frequently in many areas of higher mathematics. Both of these exist as double values: the value of pi is stored in Math.PI while the value of e is stored in Math.E.(The values are not exact, but they are correct to 16 significant digits.)

Exercise 1.10

  1. The text suggests three advantages of using named constants instead of literals. What are they?
  2. The following program attempts to change the value of a constant. Copy the program and run it to see what error message is produced.
    class BadConstantUse
    {
       public static void main (String[] args)
       {
          final int TEST = 0;
          TEST = 1;
          System.out.println(TEST);
       }
    }