Learn Java the Hard Way (Second Edition)

Exercise 10: Variables Only Hold Values

Okay, now that we can get input from the human and do calculations with it, I want to call attention to something that many of my students get confused about. The following code should compile, but it probably will not work the way you expect.

I have intentionally made a logical error in the code. It is not a problem with the syntax (the part of the code the compiler cares about), and it is not a runtime error like you get when the human types a double when the Scanner object is expecting an integer. This logical error is a flaw with how I have designed the flow of instructions, so that the output is not what I was trying to accomplish.

 
 1 import java.util.Scanner;
 2 
 3 public class Sequencing {
 4     public static void main( String[] args ) {
 5         // THIS CODE IS BROKEN UNTIL YOU FIX IT
 6 
 7         Scanner keyboard = new Scanner(System.in);
 8         double price = 0, salesTax, total;
 9 
10         salesTax = price * 0.0825;
11         total = price + salesTax;
12 
13         System.out.print( "How much is the purchase price? " );
14         price = keyboard.nextDouble();
15 
16         System.out.println( "Item price:\t" + price );
17         System.out.println( "Sales tax:\t" + salesTax );
18         System.out.println( "Total cost:\t" + total );
19     }
20 }

What You Should See

Are you surprised by the output? Did you expect the sales tax on $7.99 to show something like $0.66 instead of a big fat zero? And the total cost should have been something like $8.65, right? What happened?

What happened is that in Java (and most programming languages), variables can not hold formulas. Variables can only hold values.

Look at line 10. My students sometimes think that line stores the formula price * 0.0825 into the variable salesTax and then later the human stores the value 7.99 into the variable price. They think that on line 17 when we print out salesTax that the computer then “runs” the formula somehow.

This is not what happens. In fact, this program shouldn’t have even compiled. The variable price doesn’t even have a proper value on line 10. The only reason it does have a value is because I did something sneaky on line 8.

Normally we have been declaring variables up at the top of our programs and then initializing them later. But on line 8 I declared price and initialized it with a value of 0. When you declare and initialize a variable at the same time, that is called “defining” the variable. salesTax and total are not defined on line 8, just declared.

So then on line 14 the value the human types in doesn’t initialize price; price already has an initial value (0). But the value the human types in (7.99 or whatever) does get stored into the variable price here. The 0 is replaced with 7.99.

From line 8 until 13 the variable price contains the value 0. When line 14 begins executing and while we are waiting for the human to type something, price still contains 0. But by the time line 14 has completed, whatever the human typed has been stored into price, replacing the zero. Then from line 15 until the end of the program, the variable price contains the value 7.99 (or whatever was typed in).

So with this in mind, we can figure out what really happens on line 10. Line 10 does not store a formula into salesTax but it does store a value. What value? It takes the value of the variable price at this point in the code (which is 0), multiplies it by 0.0825 (which is still zero), and then stores that zero into salesTax.

As line 10 is beginning, the value of salesTax is undefined. (salesTax is declared but not defined.) By the end of line 10, salesTax holds the value 0. There is no line of code that changes salesTax (there is no line of code that begins with salesTax =), so that value never changes and salesTax is still zero when it is displayed on line 17.

Line 11 is similar. It takes the value of price right then (zero) and adds it to the value of salesTax right then (also zero) and stores the sum (zero) into the variable total. And total’s value is never changed, and total does not somehow “remember” that its value came from a formula involving some variables.

So there you have it. Variables hold values, not formulas. Computer programs are not a set of rules, they are a sequence of instructions that the computer executes in order, and things later in your code depend on what happened before.


“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell