Learn Java the Hard Way (Second Edition)

Exercise 5: Saving Information in Variables

Programs would be pretty boring if the only thing you could do was print things on the screen. We would like our programs to be interactive.

Unfortunately, interactivity requires several different concepts working together, and explaining them all at once might be confusing. So I am going to cover them one at a time.

First up: variables! If you have ever taken an Algebra class, you are familiar with the concept of variables in mathematics. Programming languages have variables, too, and the basic concept is the same:

“A variable is a name that refers to a location that holds a value.”

Variables in Java have four major differences from math variables:

  1. Variable names can be more than one letter long.
  2. Variables can hold more than just numbers; they can hold words.
  3. You have to choose what type of values the variable will hold when the variable is first created.
  4. The value of a variable (but not its type) can change throughout the program. For example, the variable score might start out with a value of 0, but by the end of the program, score might hold the value 413500 instead.

Okay, enough discussion. Let’s get to the code! I’m not going to tell you what the name of the file is supposed to be. You’ll have to figure it out for yourself.

 1 public class CreatingVariables {
 2     public static void main( String[] args ) {
 3         int x, y, age;
 4         double seconds, e, checking;
 5         String firstName, lastName, title;
 7         x = 10;
 8         y = 400;
 9         age = 39;
11         seconds = 4.71;
12         e = 2.71828182845904523536;
13         checking = 1.89;
15         firstName = "Graham";
16         lastName = "Mitchell";
17         title = "Mr.";
19         System.out.println( "The variable x contains " + x );
20         System.out.println( "The value " + y + " is stored in the variable y." );
21         System.out.println( "The experiment took " + seconds + " seconds." );
22         System.out.println( "A favorite irrational # is Euler's number: " + e );
23         System.out.println( "Hopefully you have more than $" + checking + "!" );
24         System.out.println( "My name's " + title + " " + firstName + lastName );
25     }
26 }

What You Should See

On lines 3 through 5 we declare3 nine variables. The first three are named x, y, and age. All three of these variables are “integers”, which is the type of variable that can hold a value between ± two billion.

A variable which is an integer could hold the value 10. It could hold the value -8192. An integer variable can hold 123456789. It could not hold the value 3.14 because that has a fractional part. An integer variable could not hold the value 10000000000 because ten billion is too big.

On line 4 we declare variables named seconds, e, and checking. These three variables are “floating-point”, which is the type of variable that can hold a number that might have a fractional part. “Double” is short for “double-precision floating-point”. In this book, I will use the terms “double” and “floating-point” interchangably. I know that’s kind of confusing, but I didn’t invent the terminology; I just teach it.

A floating-point variable could hold the value 4.71. It could hold the value -8192. (It may have a fractional part but doesn’t have to.) A double-precision floating-point variable can pretty much hold any value between ± 1.79769 × 10308 and 4.94065 × 10-324

However, doubles have limited precision. Notice that on line 12 I store the value 2.71828182845904523536 into the variable named e, but when I print out that value on line 22, only 2.718281828459045 comes out. Doubles do not have enough significant figures to hold the value 2.71828182845904523536 precisely. Integers have perfect precision, but can only hold whole numbers and can’t hold huge, huge values. (And single-precision floating-point variables (a.k.a. floats) have even fewer significant figures than doubles do. Which is why they aren’t used much any more.)

The last type of variable we are going to look at in this exercise is the String. On line 5 we declare three String variables: firstName, lastName and title. String variables can hold words and phrases; the name is short for “string of characters”.

On lines 7 through 9 we initialize4 the three integer values. The value 10 is stored into x. Before this point, the variable x exists, but its value is undefined. 400 is stored into y and 39 is stored into the variable age.

Lines 11 through 13 give initial values to the three double variables, and lines 15 through 17 initialize the three String variables. Then lines 19 through 24 display the values of those variables on the screen. Notice that the variable names are not surrounded by quotes.

I know that it doesn’t make sense to use variables for a program like this, but soon everything will become clear.



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