Skip Top Navigation Bar

Naming Variables

Data is stored for a program using 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.

Rules for Naming Variables

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 numOneName instead.
  • n is a legal Java variable name, however it isn't very descriptive. Using this variable name could 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.
Vending Machine Price
  • price
  • item_price
  • itemPrice
  • 1price
  • item price
  • price, item_price, and itemPrice all start with letters, contain no illegal character, and are not Java keywords.
  • 1price starts with a number. You could use onePrice instead.
  • item price contains a blank space. You could use itemPrice or item_price instead.

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.

Resources

Next Learn Tutorial

Learn: Declare and Initialize Variables