Comp 110 Variables

Variables are names we give to storage spaces that allow us to store, load, and modify any type of data our programs work with in memory.

Variable Declaration Statement

When a variable is declared, you give the variable a <name> and indicate its <type>. The programming language will then know, from that point forward in the same scope (see below), the name you gave it will refer to a storage space in memory reserved for it. The general form of a variable declaration looks like this:

<type> <name>;
// Examples:
String name;
int age;
boolean cheersForUNC;

Some rules for variable declaration:

  • Names must be a single word, begin with a letter*, and contain only numbers and letters
  • You cannot refer to a variable until after its declaration
  • Variables are only accessible within the curly braces that it was declared (read scope for more information)
  • You cannot declare the same variable name twice within the same set of curly braces.

Variable Assignment Statement

After declaring a variable, you can assign values to it. This means you are storing data in that variable's piece of memory. The general syntax form and some examples of assignment look like so:

<name> = <value expression>;
// Examples:
name = "Kris";
age = 31;
cheersForUNC = true;

The value expression assigned to a variable must be the same type as the variable was declared. You cannot assign a String value to an int variable, for example.

Assignment does not mean equality. Immediately after the assignment statement is run, it is true that a variable is equal to the value it was just assigned. However, variables can be assigned new values as the program continues running. Remember, variables are just places where a program stores the values it needs in order to solve the problem it is designed for. Here are some more examples:

age = 32;
age = 33;

Unlike in math, the two assignment statements above are perfectly valid. The program can assign a value of 32 to a variable whose previous value was 31. It can then immediately assign a value of 33 to it. After these two lines of code complete, the age variable is holding a value of 33. It could later be reassigned just the same.

Variable Assignment Rules:

  • The first time you assign a value to a variable it has a special name: initialization 
  • It is very important to always initialize your variables
    • In other words, make sure that your variables always have a starting variable
  • A variable's value can change as the program runs
    • Just assign another value to the same variable!
    • After an assignment statement runs, when subsequent lines of code run the variable will have the last assigned value.

Variable Initialization Statement

It is important to always assign a value to a variable that is declared. It is so important we give this first assignment a special name: initialization. This big word simply refers to the first time a value is assigned to a variable. As a verb, we say we initialize a variable. It is bad practice to use a variable without initiailizing it first.

It is so important to initialize, and so common, that Java gives us special syntax for declaring and initializing variables in a single statement:

<type> <name> = <value expression>;
// Examples:
int yearBorn = 1985;
String favoriteSchool = "UNC";
boolean goodAtBasketball = false;

Variable Reference Expressions

To read or access a value stored in a variable, you simply reference it by name. You can read a variable's value any place where you can use a value of that type. We'll see many examples of where we can use different types of data as the course continues. Here are two examples:

System.out.println(yearBorn);
age = 2016 - yearBorn;
age = age + 1; // Birthday!

Notice that in an assignment statement, you can refer to variables (even the same variable!) in the value expression.

Scope

The scope of a variable, or where a variable is declared impacts where that variable can be referenced from. In Java, the simple rule to remember is that a variable can only be referenced from within the same set of curly braces it is defined.