Chapter 5

Constants and Variables


CONTENTS

If there's one thing that every computer program has in common, it's that they always process data input and produce some sort of output based on that data. And because data is so important to a computer program, it stands to reason that there must be plenty of different ways to store data so that programs can do their processing correctly and efficiently. In order to keep track of data, programs use constants and variables. In this chapter, you discover what constants and variables are, as well as learn to use them in the Java language.

Constants

If you think about the term "constant" for a few moments, you might conclude that constants must have something to do with data that never changes. And your conclusion would be correct. A constant is nothing more than a value, in a program, that stays the same throughout the program's execution. However, while the definition of a constant is fairly simple, constants themselves can come in many different guises. For example, the numeral 2, when it's used in a line of program code, is a constant. If you place the word "Java" in a program, the characters that comprise the word are also constants. In fact, these constant characters taken together are often referred to as a string constant.

NOTE
To be entirely accurate, I should say that text and numerals that are placed in program code are actually called literals, because the value is literally, rather than symbolically, in the program. If this literal and symbolic stuff is confusing you, you'll probably have it figured out by the end of this chapter. For now, just know that I'm lumping literals in with constants to simplify the discussion.

Such values as the numeral 2 and the string constant "Java" are sometimes called hard-coded values because the values that represent the constants are placed literally in the program code. For example, suppose you were writing a program and wanted to calculate the amount of sales tax on a purchase. Suppose further that the total purchase in question is $12.00 and the sales tax in your state is 6 percent. The calculation that'll give you the sales tax would look like this:


tax = 12 * .06;

Suppose now that you write a large program that uses the sales tax percentage in many places. Then, after you've been happily using your program for a few months, the state suddenly decides to raise the sales tax to seven percent. In order to get your program working again, you have to go through every line of code, looking for the .06 values that represent the sales tax and changing them to .07. Such a modification can be a great deal of work in a large program. Worse, you may miss one or two places in the code that need to be changed, leaving your program with some serious bugs.

To avoid these situations, programmers often use something called symbolic constants, which are simply words that represent values in a program. In the case of your sales tax program, you could choose a word like SALESTAX (no spaces) to represent the current sales tax percentage for your state. Then, at the beginning of your program, you set SALESTAX to be equal to the current state sales tax. In the Java language, such a line of program code might look like this:


final float SALESTAX = 0.06;

In the preceding line, the word final tells Java that this data object is going to be a constant. The float is the data type, which, in this case, is a floating point. (You'll learn more about data types later in this chapter.) The word SALESTAX is the symbolic constant. The equals sign tells Java that the word on the left should be equal to the value on the right, which, in this case, is 0.06.

After defining the symbolic constant SALESTAX, you can rewrite any lines that use the current sales tax value to use the symbolic constant rather than the hard-coded value. For example, the calculation for the sales tax on that $12.00 purchase might now look something like this:


tax = 12 * SALESTAX;

TIP
In order to differentiate symbolic constants from other values in a program, programmers often use all uppercase letters when naming these constants.

Now, when your state changes the sales tax to 7 percent, you need only change the value you assign to the symbolic constant and the rest of the program automatically fixes itself. The change would look like this:


final float SALESTAX = 0.07;

Variables

If constants are program values that cannot be changed throughout the execution of a program, what are variables? Variables are values that can change as much as needed during the execution of a program. Because of a variable's changing nature, there's no such thing as a hard-coded variable. That is, hard-coded values in a program are always constants (or, more accurately, literals).

Why do programs need variables? Think back to the sales tax program from the previous section. You may recall that you ended up with a program line that looked like this:


tax = 12 * SALESTAX;

In this line, the word tax is a variable. So, one reason you need variables in a program is to hold the results of a calculation. In this case, you can think of the word tax as a kind of digital bucket into which the program dumps the result of its sales tax calculation. When you need the value stored in tax, you can just reach in and take it out-figuratively speaking, of course. As an example, to determine the total cost of a $12.00 purchase, plus the sales tax, you might write a line like this:


total = 12 + tax;

In this line, the word total is yet another variable. After the computer performs the requested calculation, the variable total will contain the sum of 12 and whatever value is stored in tax. For example, if the value 0.72 is stored in tax, after the calculation, total would be equal to 12.72.

Do you see another place where a variable is necessary? How about the hard-coded value 12? Such a hard-coded value makes a program pretty useless because every person that comes into your store to buy something isn't going to spend exactly $12.00. Because the amount of each customer's purchase will change, the value used in your sales tax calculation must change, too. That means you need another variable. How about creating a variable named purchase? With such a variable, you can rewrite the calculations like this:


tax = purchase * SALESTAX;

total = purchase + tax;

Now you can see how valuable variables can be. Every value in the preceding lines is represented by a variable. (Although you're using SALESTAX as a symbolic constant, because of Java's current lack of true constants, it's really a variable, too.) All you have to do to get the total to charge your customer is plug the cost of his purchase into the variable purchase, something you'll see how to do in Chapter 6 "Simple Input and Output."

Math-savvy readers may have already figured that the preceding two lines can be easily simplified into one, like this:


total = purchase + (purchase * SALESTAX);

This revised line of code, however, is not as easy to understand as the original two lines were. In programming, you must constantly decide between longer, simpler code and shorter, more complex code. I tend to go for the longer, easy-to-understand approach, except when the longer code might bog down the program.

Naming Constants and Variables

The first computer languages were developed by mathematicians. For that reason, the calculations and the variables used in those calculations were modeled after the types of equations mathematicians were already accustomed to working with. For example, in the old days, the lines in your tax program might have looked like this:


a = b * c;

d = b + a;

As you can see, this type of variable-naming convention left a lot to be desired. It's virtually impossible to tell what type of calculations are being performed. In order to understand their own programs, programmers had to use tons of comments mixed in with their source code so they could remember what the heck they were doing from one programming session to the next. Such a section of source code in Java might look like Listing 5.1.


Listing 5.1  LST5_1.TXT: An Example of Mathematician's Variables.

// Calculate the amount of sales tax.

a = b * c;



// Add the sales tax to the purchase amount.

d = b + a;


Although adding comments to the program lines helps a little, the code is still pretty confusing, because you don't really know what the variables a, b, c, and d stand for. After a while (probably when mathematicians weren't the only programmers), someone came up with the idea of allowing more than one character in a variable name, which would enable the programmer to create mathematical expressions that read more like English. Thus, the confusing example above would be written as Listing 5.2.


Listing 5.2  LST5_2.TXT: Using English-like Variable Names.

// Calculate the amount of sales tax.

tax = purchase * SALESTAX;



// Add the sales tax to the purchase amount.

total = purchase + tax;


By using carefully chosen variable names, you can make your programs self documenting, which means that the program lines themselves tell whoever might be reading the program what the program does. If you strip away the comments from the preceding example, you can still see what calculations are being performed.

Of course, there are rules for choosing constant and variable names (also known as identifiers because they identify a program object). You can't just type a bunch of characters on your keyboard and expect Java to accept them. First, every Java identifier must begin with one of these characters:

A-Z
a-z
_
$

The preceding characters are any uppercase letter from A through Z, any lowercase letter from a through z, an underscore, and the dollar sign.

Following the first character, the rest of the identifier can use any of these characters:

A-Z
a-z
_
$
0-9

As you may have noticed, this second set of characters is very similar to the first. In fact, the only difference is the addition of the digits from 0 through 9.

NOTE
Java identifiers can also use Unicode characters above the hexadecimal value of 00C0. If you don't know about Unicode characters, don't panic; you won't be using them in this book. Briefly put, Unicode characters expand the symbols that can be used in a character set to include characters that are not part of the English language.

Using the rules given, the following are valid identifiers in a Java program:


number

number2

amount_of_sale

$amount

The following identifiers are not valid in a Java program:


1number

amount of sale

&amount

item#

Example: Creating Your Own Identifiers

Suppose that you're now ready to write a program that calculates the total number of parking spaces left in a parking garage. You know that the total number of spaces in the garage is 100. You further know that the vehicles in the garage are classified as cars, trucks, and vans. The first step is to determine which values would be good candidates for constants. Because a constant should represent a value that's not likely to change from one program run to another, the number of vehicles that the garage can hold would make a good constant. Thinking hard (someone smell wood burning?), you come up with an identifier of TOTALSPACES for this value. In Java, the constant's definition looks like this:


final int TOTALSPACES = 100;

In this line, the keyword int represents the data type, which is integer. You should be able to understand the rest of the line.

Now, you need to come up with the mathematical formula that'll give you the answer you want. First, you know that the total number of vehicles in the garage is equal to the sum of the number of cars, trucks, and vans in the garage. Stating the problem in this way not only clarifies what form the calculation must take, but also suggests a set of good identifiers for your program. Those identifiers are cars, trucks, vans, and total_vehicles. So, in Java, your first calculation looks like this:


total_vehicles = cars + trucks + vans;

The next step is to subtract the total number of vehicles from the total number of spaces that the garage holds. For this calculation, you need only one new identifier to represent the remaining spaces, which is the result of the calculation. Again, stating the problem leads to the variable name, which might be remaining_spaces. The final calculation then looks like this:


remaining_spaces = TOTALSPACES - total_vehicles;

Data Types

In attempting to give you a quick introduction to constants and variables, the preceding sections skipped over a very important attribute of all constants and variables: data type. You may remember my mentioning two data types already, these being floating point (represented by the float keyword) and integer (represented by the int keyword). Java has eight different data types, all of which represent different kinds of values in a program. These data types are byte, short, int, long, float, double, char, and boolean. In this section, you'll learn what kinds of values these various data types represent.

Integer Values

The most common values used in computer programs are integers, which represent whole number values such as 12, 1988, and -34. Integer values can be both positive or negative, or even the value 0. The size of the value that's allowed depends on the integer data type you choose. Java features four integer data types, which are byte, short, int, and long. Although some computer languages allow both signed and unsigned integer values, all of Java's integers are signed, which means they can be positive or negative. (Unsigned values, which Java does not support, can hold only positive numbers.)

The first integer type, byte, takes up the least amount of space in a computer's memory. When you declare a constant or variable as byte, you are limited to values in the range -128 to 127. Why would you want to limit the size of a value in this way? Because the smaller the data type, the faster the computer can manipulate it. For example, your computer can move a byte value, which consumes only eight bits of memory, much faster than an int value, which, in Java, is four times as large.

In Java, you declare a byte value like this:


byte identifier;

In the preceding line, byte is the data type for the value, and identifier is the variable's name. You can also simultaneously declare and assign a value to a variable like this:


byte count = 100;

After Java executes the preceding line, your program will have a variable named count that currently holds the value of 100. Of course, you can change the contents of count at any time in your program. It only starts off holding the value 100.

The next biggest type of Java integer is short. A variable declared as short can hold a value from -32,768 to 32,767. You declare a short value like this:


short identifier;

or


short identifier = value;

In the preceding line, value can be any value from -32,768 to 32,767, as described previously. In Java, short values are twice as big in memory-16 bits (or two bytes)-as byte values.

Next in the integer data types is int, which can hold a value from -2,147,483,648 to 2,147,483,647. Now you're getting into some big numbers! The int data type can hold such large numbers because it takes up 32 bits (four bytes) of computer memory. You declare int values like this:


int identifier;

or


int identifier = value;

The final integer data type in the Java language is long, which takes up a whopping 64 bits (eight bytes) of computer memory and can hold truly immense numbers. Unless you're calculating the number of molecules in the universe, you don't even have to know how big a long number can be. I'd figure it out for you, but I've never seen a calculator that can handle numbers that big. You declare a long value like this:


long identifier;

or


long identifier = value;

TIP
How do you know which integer data type to use in your program? Choose the smallest data type that can hold the largest numbers you'll be manipulating. Following this rule keeps your programs running as fast as possible. However, having said that, I should tell you that most programmers (including me) use the int data type a lot, even when they can get away with a byte.

Floating-Point Values

Whereas integer values can hold only whole numbers, the floating-point data types can hold values with both whole number and fractional parts. Examples of floating-point values include 32.9, 123.284, and -43.436. As you can see, just like integers, floating-point values can be either positive or negative.

Java includes two floating-point types, which are float and double. Each type allows greater precision in calculations. What does this mean? Floating-point numbers can become very complex when they're used in calculations, particularly in multiplication and division. For example, when you divide 3.9 by 2.7, you get 1.44444444. In actuality, though, the fractional portion of the number goes on forever. That is, if you were to continue the division calculation, you'd discover that you keep getting more and more fours in the fractional part of the answer. The answer to 3.9 divided by 2.7 is not really 1.44444444, but rather something more like 1.4444444444444444. But even that answer isn't completely accurate. A more accurate answer would be 1.44444444444444444444444444444444. The more 4s you add to the answer the more accurate the answer becomes-yet, because the 4s extend on into infinity, you can never arrive at a completely accurate answer.

Dealing with floating-point values frequently means deciding how many decimal places in the answer is accurate enough. That's where the difference between the float and double data types shows up. In Java, a value declared as float can hold a number in the range from around -3.402823 x 10 38 to around 3.402823 x 10 38. These types of values are also known as single-precision floating-point numbers and take up 32 bits (four bytes) of memory. You declare a single-precision floating-point number like this:


float identifier;

or


float identifier = value;

In the second line, value must be a value in the range given in the previous paragraph, followed by an upper- or lowercase F. However, you can write floating-point numbers in a couple of ways, using regular digits and a decimal point or using scientific notation. This value is the type of floating-point number you're used to seeing:

356.552

Now, here's the same number written using Java's rules, in both the number's normal form and in the form of scientific notation:

356.552f
3.56552e2f

Both of the preceding values are equivalent, and you can use either form in a Java program. The e2 in the second example is the equivalent of writing x 102 and is a short form of scientific notation that's often used in programming languages.

NOTE
If you're not familiar with scientific notation, the value 3.402823 x 10 38 is equal to 3.402823 times a number that starts with a 1 and is followed by 38 zeroes. Computer languages shorten this scientific notation to 3.402823e38.

The second type of floating-point data, double, represents a double-precision value, which is a much more accurate representation of floating-point numbers because it allows for more decimal places. A double value can be in the range from -1.79769313486232 x 10 308 to 1.79769313486232 x 10 308 and is declared like this:


double identifier;

or


double identifier = value;

Floating-point values of the double type are written exactly as their float counterparts, except you use an upper- or lowercase D as the suffix, rather than an F. Here's a few examples:

3.14d
344.23456D
3.4423456e2d

TIP
When using floating-point numbers in your programs, the same rule that you learned about integers applies: Use the smallest data type you can. This is especially true for floating-point numbers, which are notorious for slowing computer programs to a crawl. Unless you're doing highly precise programming, such as 3-D modeling, the single-precision float data type should do just fine.

Character Values

Often in your programs, you'll need a way to represent character values rather than just numbers. A character is a symbol that's used in text. The most obvious examples of characters are the letters of the alphabet, in both upper- and lowercase varieties. There are, however, many other characters, including not only things such as spaces, exclamation points, and commas, but also tabs, carriage returns, and line feeds. The symbols 0 through 9 are also characters when they're not being used in mathematical calculations.

In order to provide storage for character values, Java features the char data type, which is 16 bits. However, the size of the char data type has little to do with the values it can hold. Basically, you can think of a char as being able to hold a single character. (The 16 bit length accommodates Unicode characters, which you don't need to worry about in this book.) You declare a char value like this:


char c;

or


char c = 'A';

In the second example, you're not only declaring the variable c as a char, but also setting its value to an uppercase A. Notice that the character that's being assigned is enclosed in single quotes.

Some characters cannot be written with only a single symbol. For example, the tab character is represented in Java as \t, which is a backslash followed by a lowercase t. There are several of these special characters, as shown in Table 5.1.

Table 5.1  Special Character Literals.

Character
Symbol
Backslash
\\
Backspace
\b
Carriage return
\r
Double quote
\"
Form feed
\f
Line feed
\n
Single quote
\'
Tab
\t

Although the special characters in Table 5.1 are represented by two symbols, the first of which is always a backslash, you still use them as single characters. For example, to define a char variable as a backspace character, you might write something like the following in your Java program:


char backspace = '\b';

When Java's compiler sees the backslash, it knows that it's about to encounter a special character of some type. The symbol following the backslash tells the compiler which special character to use. Because the backslash is used to signify a special character, when you want to specify the backslash character yourself, you must use two backslashes, which keeps the compiler from getting confused. Other special characters that might confuse the compiler because they are used as part of the Java language are single and double quotes. When you want to use these characters in your program's data, you must also precede them with a backslash.

Boolean Values

Many times in a program, you need a way to determine if a specific condition has been met. For example, you might need to know whether a part of your program executed properly. In such cases, you can use Boolean values, which are represented in Java by the boolean data type. Boolean values are unique in that they can be only one of two possible values: true or false. You declare a boolean value like this:


boolean identifier;

or


boolean identifier = value;

In the second example, value must be true or false. In an actual program, you might write something like this:


boolean file_okay = true;

Boolean values are often used in if statements, which enable you to do different things depending on the value of a variable. You'll learn about if statements in Chapter 9, "The if and switch Statements."

Table 5.2 summarizes Java's various data types. Take some time now to look over the table and make sure you understand how the data types differ from each other. You might also want to think of ways you might use each data type in an actual program.

Table 5.2  Summary of Java's Data Types.

TypeValue
byte-128 to 127
short-32,768 to 32,767
int-2,147,483,648 to 2,147,483,647
longHuge
float-3.402823e38 to 3.402823e38
double-1.79769313486232e308 to 1.79769313486232e308
charSymbols used in text
booleanTrue or false

Variable Scope

When you write your Java programs, you can't just declare your variables willy-nilly all over the place. You first have to consider how and where you need to use the variables. This is because variables have an attribute known as scope, which determines where in your program variables can be accessed. In Java, a variable's scope is determined by the program block in which the variable first appears. The variable is "visible" to the program only from the beginning of its program block to the end of the program block. When a program's execution leaves a block, all the variables in the block disappear, a phenomenon that programmers call "going out of scope."

Now you're probably wondering, "What the devil is a program block?" Generally, a program block is a section of program code that starts with an opening curly brace ({) and ends with a closing curly brace (}). (Sometimes, the beginning and ending of a block are not explicitly defined, but you don't have to worry about that just yet.) Specifically, program blocks include things like classes, functions, and loops, all of which you'll learn about later in this book.

Of course, things aren't quite as simple as all that (you're dealing with computers, after all). The truth is that you can have program blocks within other program blocks. When you have one block inside another, the inner block is considered to be nested. Figure 5.1 illustrates the concept of nested program blocks.

Figure 5.1 : Program blocks can be nested inside other program blocks.

In the figure, Block 1 encloses both Block 2 and Block 3. That is, Block 2 and Block 3 are nested within Block 1, because these blocks occur after Block 1's opening brace but before Block 1's closing brace. If you wanted, you could also create a Block 4 and nest it within Block 2 or Block 3, and thus create even another level of nesting. As you'll see when you start writing full-length Java programming, all programs have a lot of nesting going on.

The ability to nest program blocks adds a wrinkle to the idea of variable scope. Because a variable remains in scope from the beginning of its block to the end of its block, such a variable is also in scope in any blocks that are nested in the variable's block. For example, looking back at Figure 5.1, a variable that's defined in Block 1 is accessible in not just Block 1, but also in Block 2 and Block 3. However, a variable defined inside Block 2 is accessible only in Block 2, because such a variable goes into scope at the start of Block 2 and goes out of scope at the end of Block 2. If you're a little confused, the following example ought to clear things up.

Example: Determining a Variable's Scope

Suppose you've written the small Java program shown in Listing 5.3. (Nevermind, at this point, that you don't know much about writing Java programs. Such minor details will be remedied by the time you complete this book.) The program shown in the listing follows the same program structure as that shown in Figure 5.1. That is, there is one large main block that contains two nested blocks. The main block begins with the opening brace on the second line and ends with the closing brace at the end of the program. The first inner block begins with the opening brace after the line labeling Function1 and ends with the closing brace three lines below the opening brace. The second inner block is defined similarly, with its own opening and closing braces.


Listing 5.3  LST5_3.TXT: Determining Variable Scope.

public class Block1 extends Applet

{

    int value1 = 32;



    void Block2()

    {

        float value2 = 4.5f;

        value1 = 45;

    }



    void Block3()

    {

        value1 = 100;



        // The following line causes an error.

        value2 = 55.46f;

    }

}


Now look at the variables being used in this program. The first variable defined in the program is value1, which is found right after the main block's opening brace. This means that value1 is accessible in all three blocks, as you can see by looking at the Block2 and Block3 blocks, both of which assign new values to value1.

The second variable in the program, value2, is defined inside the Block2 block, where it's both declared and assigned the value 4.5f. In the Block3 block, the program tries to assign a value to value2. If you tried to compile this program, you'd see that this line creates an error message, as shown in Figure 5.2. In the figure, the compiler is insisting that value2 in the Block3 block is undefined, which, of course, is true as far as the Block3 block is concerned. You'd get a similar message if you tried to access value2 anywhere but within the scope of the Block2 block.

Figure 5.2 : When you try to access a variable that's out of scope, Java's compiler thinks that the variable is undefined.

You can use variable scope to simplify the access of variables in a program. For example, you will usually declare variables that you need in many places, so that they are in scope in the entire class. That way, you can access the variables without having to pass them as arguments to functions. (If you don't know about argument passing just yet, you will after you read Chapter 12, "Functions.") On the other hand, you'll have lots of variables that you use only inside one particular program block. You can keep your program uncluttered by being sure to declare these types of variables only in the blocks in which they're used. You'll learn more about setting up variables with the proper scope as you write Java programs later in this book.

Summary

All computers must manipulate data in order to produce output. Java, like all programming languages, features many data types that you can use for constants and variables in your programs. These data types enable you to store everything from simple integers like 23 and -10 to strings and complex floating-point numbers. There's a lot to know about variables, so your head may be spinning a bit at this point. Rest assured, however, that once you start writing programs and using variables, all the theoretical stuff will make sense.

Review Questions

  1. What is a constant?
  2. What is a variable?
  3. How do constants and variables make writing programs easier?
  4. Name the eight data types used in Java.
  5. What is variable scope?

Review Exercises

  1. Suppose you need to write a Java program that calculates an employee's paycheck for a given number of hours of work. Write declarations for the variables you'll need.
  2. Using the variables you declared in exercise 1, write the program lines needed to perform the paycheck calculations.
  3. Using Figure 5.1 as a guide, create a new figure that adds a program block to the class such that any variables declared in the new block cannot be accessed in any other program block.
  4. Using the modified figure, add yet another program block, this time adding an additional level of block nesting.