Skip Top Navigation Bar

Declaring and Initialing Variables

Overview

In this lesson, students will begin to develop an understanding of the different types of data that are stored by the computer.

Learning Objectives

Skills

Student Outcomes

Students will be able to:

Duration: 1 class period

Resources

Background

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
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.
Someone's birthday
  • dateOfBirth
  • birthday
  • bday
  • 0513birthday
  • birthd@y
  • dateOfBirth, birthday, and bday all start with letters, contain no illegal character, and are not Java keywords. Be careful of bday as it is less descriptive than the other two variable names.
  • 0513birthday starts with a number. You could use birthday0513 instead.
  • birthd@y contains the symbol @, which is not a legal character to use. You could use birthday instead.
A restaurant location
  • restLocation
  • location
  • storeLoc
  • rl
  • restaurant location
  • restLocation, location, and storeLoc all start with letters, contain no illegal character, and are not Java keywords. If this app keeps track of the restaurant location and that user's location, the variable location might not be descriptive enough.
  • rl while technically correct, is not a very descriptive variable name.
  • restaurant location contains a blank space. You could use restaurantLocation 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 itemPrice. This is called camel-case.

Declaring and Initializing a Variable

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

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

Description Creation of Variable
Vending Machine Price double itemPrice = 1.99;
Current Game Score int yourScore = 0;
Direction of a Robot int direction = 90;
Last Name Initial char intial = 'S';
Whether a System is Ready or Not boolean isSystemReady = true;

Boolean variables can be assigned true or false. The words true and false are Java keywords.*

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', '-'.

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.

Activities

Activity 1: Creating Variables

Have students revisit their data identifying activities from earlier and expand on that to create variables. They should consider the name and ensure that it will be descriptive of the value it will hold and consider the initial value that will be stored.