Variables are a powerful tool, but sometimes you want to use a defined value, one whose value you want to ensure remains constant. A constant is like a variable in that it can store a value. However, unlike a variable, you cannot change the value of a constant while the program runs.
For example, you might need to work with the Fahrenheit freezing and boiling points of water in a program simulating a chemistry experiment. Your program will be clearer if you name the variables that store these values
BoilingPoint, but you do not want to permit their values to be changed while the program is executing. The solution is to use a constant. Constants come in three flavors: literals, symbolic constants, and enumerations.
A literal constant is just a value. For example, 32 is a literal constant. It does not have a name; it is just a literal value. And you can’t make the value 32 represent any other value. The value of 32 is always 32. You can’t assign a new value to 32, and you can’t make 32 represent the value 99 no matter how hard you might try. You’ll use literal constants a lot, but you probably won’t think of them as such.
Symbolic constants assign a name to a constant value. You declare a symbolic constant using the following syntax:
const keyword is followed by a type, an identifier, the assignment operator (
=), and the value to assign to the constant.
This is similar to declaring a variable ...