Declaring Variables in Java
The ListLetters program reads a line of text entered by the user and stores it in a String variable named str.
A Line of Text Goes Somewhere
The ListLetters program is part of a Nested for Loops Exercise. According to its own comments, it reads a line of text entered by the user, prints a list of the letters that occur in that text, and reports how many different letters were found. To do any of this, the program needs a place to keep the text it receives, a place to keep a running count, and a place to hold a single letter while it examines the text. Those places are variables, and they are declared at the start of main before the program does any work.
What do you think happens?
Suppose the user types the line of text Hello and the program has just finished its declarations. What should str contain, and what should letter contain? Write your answer down before reading on.
Reveal answer
str holds the whole line Hello, and letter holds one letter at a time.
str is a String, so it can hold the whole line. letter is a char, so it holds one letter at a time. Your answer should follow from the declarations alone, before any code runs.
Matching Each Variable to Its Job
A declaration tells the compiler what kind of value a variable will hold. The source states that variables are declared with specific data types, such as String, int, and char, and that each type determines what kind of value the variable can hold. In ListLetters, the three declarations are String str, int count, and char letter. Each type was chosen to match the job that its variable performs.
| Declaration | Type | Intended purpose | Why this type fits |
|---|---|---|---|
| String str | String | Holds the line of text the user entered | The whole line is text, so it needs a type that holds a string of characters |
| int count | int | Holds how many different letters were found | The result is a number that is counted and reported |
| char letter | char | Holds one letter while the program examines it | The source describes letter as holding one letter at a time |
The three variables of ListLetters, their declared types, and the purposes stated for them
Reading the Anatomy of a Declaration
The general form of a declaration in this program is a type followed by a variable name, ending with a semicolon. The pattern is written as type-name, then variable-name-or-names, then a semicolon. Each part of String str; has a specific role. The type says what kind of value the variable holds. The name is how the rest of the program refers to that variable. The semicolon ends the statement.
The pattern also allows more than one name in a single declaration, separated by commas. Good style, however, is to declare one variable per statement unless the variables are closely related. This keeps each declaration easy to read and easy to comment.
Why Declaration Comes First
Declaration is the required introduction of a variable before its use. It introduces the variable's name and tells the program what kind of variable it is.
A Java program cannot use a variable until that variable has been declared. When the declaration is executed, the computer sets aside memory for the variable and associates its name with that memory. So a declaration has two connected effects: it introduces a name the program can recognize, and it causes storage to be set aside for the value. This is why str must be declared before any text is read into it. The declaration is what gives the program a named place to put the user's input.
Tracing the Declarations in Order
Now trace the program. The declarations run in the order they are written, and each one adds a named variable to the program. The diagram below shows how the set of declared variables grows as the three declarations at the start of main are executed. Keep in mind that each variable also has a comment stating its purpose, and those purposes are what you will check values against.
Finding where a result diverges
The program has been run with the input Hello. The printed count of different letters is wrong. Using the stated purposes, where should you look for the error?
- 1Check str first
str should hold the whole line Hello, because its purpose is to hold the line the user typed. Confirm that the value after input is exactly that line.
- 2If str is wrong
The problem lies in reading input, because str never received the line the user typed. Look at how the text gets into str.
- 3If str is correct
Input reading is not the cause. The divergence must come from the work done after the text is read, such as the code that updates count.
- 4Check count against its purpose
count should equal the number of different letters found in Hello. Compare the value the program produced against that expected number to decide where the mismatch begins.
Because str is correct in this run, the divergence is in the processing after input, not in the declaration of str. The stated purposes turn a vague wrong answer into a specific place to look.
Use Before Declaration
Because a declaration introduces the name and sets aside storage, a use of that name that appears before its declaration is not allowed. The compiler reads the program in order, so it has no variable to connect to the name yet. The following generated example shows the wrong order.
- The use of str appears before its declaration, so the variable has not been introduced yet.
- Reversing the two lines puts the declaration first, which is the required order.
Naming and Commenting Declarations
Clear declarations usually separate unrelated variables and include useful comments. A declaration should carry a comment when the variable's purpose, or other useful information, would help a human reader. In ListLetters, the comment on each declaration is what makes the purposes checkable, so writing those comments is part of writing the program well.
- Each variable is declared on its own line, one per statement.
- Each comment states the purpose the variable must serve, so it can be checked during tracing.
- The names describe the job, so str, count, and letter are easy to connect with the purposes.
Declare one variable per statement, give each declaration a comment that states its purpose, and choose a type that matches the job the variable performs.
Mistakes Beginners Make With Declarations
Common mistake Using a variable before declaring it
What it looks likePrinting str on a line that appears above String str;
Why it failsA variable is introduced by its declaration, so an earlier use refers to nothing the program has established.
Do this insteadPlace the declaration before the first use of the name.
Common mistake Declaring several unrelated variables on one line
Why it failsUnrelated variables packed into one statement are harder to read and harder to comment on individually.
Do this insteadDeclare one variable per statement unless the variables are closely related, and comment each one.
Common mistake Choosing a type that does not match the job
What it looks likeDeclaring the line of text as a single char
Why it failsA char holds one letter at a time, so it cannot hold the whole line the user typed.
Do this insteadPick the type from the job: String for the line, char for one letter, and a numeric type for the count.
Common mistake Leaving a declaration without a purpose comment
Why it failsWithout a stated purpose there is nothing to check values against when the result is unexpected.
Practice Tracing Declarations
Suppose a learner writes the declarations for ListLetters as int str; char count; String letter; What is wrong with each declaration when you compare it to the job its variable must perform? Name the variable whose type or purpose does not match, and state the correct type for it.
Hints
- Compare each declared type with the value the variable must hold: the line of text, the number of letters, or one letter.
- The purpose column in the table names the job for each variable.
- Check whether a String can hold a whole line and whether a char holds one letter at a time.
Key Takeaways
- A Java declaration gives a variable a type and a name, and ends with a semicolon; in ListLetters the three declarations are String str, int count, and char letter.
- Each type is chosen to match its variable's job, so the declared type tells you what kind of value the variable should hold.
- A variable must be declared before it is used, because the declaration introduces the name and causes memory to be set aside for it.
- The stated purpose of each variable is a checklist: when output is unexpected, compare each actual value against its purpose to find where the result diverged.
- Declare one variable per statement and comment each declaration's purpose so that the program can be read and traced accurately.