Skip Top Navigation Bar

Declaring and Initializing Variables

Data is stored for a program in variables. A variable is a named storage location.

Naming Variables

Unlike math class where you use a single letter as a variable name, in programming, we strive to have the code be as readable as possible by using longer, more descriptive variable names. Single letter variable names are acceptable, but not very readable and should be avoided in many cases. Variable names are case-sensitive, meaning lower case letters and upper case letters are treated as distinctly different characters and are not interchangeable. For example, myName and myname are completely different variable names.

There are a few rules to follow when naming a variable:

  1. they can consist of letters, numbers, underscore (_), and dollar sign ($). By convention, the dollar sign ($) is never used.
  2. they cannot start with a number. By convention, they always start with a letter.
  3. they cannot be a Java keyword. A Java keyword is a word that is set aside with a specific meaning in the language. Our primitive data types, for example, are all considered Java keywords.

By convention, variables will start with a lower-case letter.

Some examples of variable names:

Description Acceptable Variable Names Unacceptable Variable Names Explanation of Example
Textbook price
  • priceOfTextbook
  • text_price
  • textbookPrice
  • $price
  • textbook price
  • priceOfTextbook, text_price, and textbookPrice all start with letters, contain no illegal character, and are not Java keywords.
  • $price uses the `$` to start the variable name. While this is technically legal, it breaks the Java convention for variable names..
  • textbook price contains a blank space. You could use textPrice or txt_price instead.
Someone's first name
  • firstName
  • nameFirst
  • name
  • 1stName
  • #1Name
  • n
  • firstName, nameFirst, and name all start with letters, contain no illegal character, and are not Java keywords. Be careful of name as it is less descriptive than the other two variable names.
  • 1stName starts with a number. You could use name1st instead.
  • #1Name contains the symbol #, which is not a legal character to use. You could use noOneName instead.
  • n is a legal Java variable name, however it isn't very descriptive. Using this variable name will cause confusion later when someone attempts to read the code and isn't sure what it stands for.
Whether an order is a combo meal or not
  • isCombo
  • is_Combo
  • comboMeal
  • isCombo?
  • ComboMeal
  • isCombo, is_combo, and comboMeal all start with letters, contain no illegal character, and are not Java keywords.
  • isCombo? contains the symbol ?, which is not a legal character to use. You could use isCombo instead.
  • ComboMeal while it follows the rules for a variable, it fails to follow the Java convention that a variable name should start with a lowercase letter.

You will notice that since we cannot use blank space the underscore (_) symbol is used to space out the words for readability. Likewise, you can use a mix of lowercase and uppercase letters to break up the words, starting each subsequent word with an uppercase letter. Such as comboMeal. This is called camel-case.

Declaring and Initializing a Variable

There are two parts to declaring and initializing a local variable.

  • specify the type and then name your variable
  • provide the variable with an initial value

We use the = to assign a value to a variable. The format that we follow is as follows:

type variableName = value;

All statements end in a ;.

Examples of Primitive Variables

  • a variable to the price of a textbook might be:

double textbookPrice = 49.99;

  • a variable for whether the meal is a combo meal or not might be:

boolean isCombo = false;

Note: boolean variables can be assigned true or false. The words true and false are Java keywords.

  • a variable for a person's first initial might be:

char firstInitial = 'C';

Character variables are assigned a character literal. A literal is a fixed value. A character literal is enclosed in single quotes. For following are examples of character literals: '7', 'A', 'b', '-'.

Let's try this out in the Java Playground. Toggle the Console to "Detailed Output" to get an understanding of what is happening in the computer.

Modify the code above to declare and initialize other variables.

A Little About Storage

For primitive data (int, double, char, boolean), the value is stored in memory and the location is named with the variable name. For non-primitive data, the data is stored in memory and either this location is named with the variable name or the variable name could contain a reference to the storage location where the data is being stored.

Resources