Logout

Home Topic 4

Variables


Variable & Declaring Variables

A variable is a named shortcut for a piece of data stored in computer memory. And the value which it represents can vary, hence the name. We term the variable name itself the variable identifier.

A variable is declared in Java by pairing up a data type (primitive or object type) and a valid variable identifier. For example:

int i;
String name;
Student s;
boolean isNight;

Variable Naming

Variable names in various languages must adhere to certain rules in their naming. In Java, the main rules are that there can be no spaces between different parts of the variable name, and variables cannot start with a number. The other thing to be careful about is to not try to name a variable a name that is already taken as a Java reserved word, like if, or for.

Though not rules, there are also conventions, which one should adhere to when choosing variable identifiers, particularly;

- starting with a lower case letter

- using "camelCase" in which the first letter of each word after the first one is capitalized

- making the variable identifier a clear and logical choice considering what it is representing

- not making the variable identifier too long, up around a maximum of fourteen or fifteen characters

So the following are good variable names:

And the following are not good variable name:


Assigning Values to Variables

We assign values to variables in Java with the assignment operator. The variable name is to the left of the assignment operator, and the value to assign to the variable is to the right of the assignment operator. The assignment operator in Java is the equals sign. (So note that = in Java does not mean "equals", it means "is assigned". Usually, in Java == is used for testing equality between two things.)

For example:

int i;
i = 42;

String name;
name = "John";

Student s;
s = new Student("Sally", true, 17, 12);

boolean isNight;
isNight = false;

Or you can do both the declaration of the variable and it's assignment all in one line:

int i = 42;
String name = "John";
Student s = new Student("Sally", true, 17, 12);
boolean isNight = false;

 

Variables Vary

As the name implies, variables vary, or at least they are allowed to vary. So the following is all valid:

int i = 42;
i = 43;
i = 52334;
i = 979779;
String name = "John";
Student s = new Student("Sally", true, 17, 12);
boolean isNight = false;
name = "John Smith";
s = new Student ("Samantha, false, 16, 11);
isNight = true;

At any given point in a program, the value of the variable is whatever last value it was assigned. So, for example, by the end of the program, the value of i is 979779, and the value of isNight is true.

Note that when using the same variable in multiple places within a program, you do not, in fact, cannot, redefine it, i.e. declare its type and name. So you cannot do this:

int i = 42;
int i = 43; //ERROR

Rather, you declare the type once, and from then on only use the variable identifier. Like this:

int i = 42;
i = 43;


Declared, assigned, used, re-assigned

A good way to get your head around variables is to look through some good solid code, and identify all of the variables. In particular, you will want to ask yourself with each occurence of them are they, at that point:

Consider the following code:

int x; // x is declared (which can only occur once per block of code)
int y;
x = 4; // x is assigned a value
for(int i = 0; i < y; i++){
    System.out.println("The value of x is " + x); // x is used
}
x = 78; // x is re-assigned
for(int i = 0; i < y; i++){
    System.out.println("The value of x is " + x); // x is used again
}

And take a look at some more involved code to do the same sort of exercises for as many variables as you can. Try the "4.3 ____" IntelliJ projects linked on the main Topic 4.3 page.

 

Variables vs. Constants

Constants are values which do not, and cannot change in a program. See the notes on constants, but basically, when you declare a "variable" which should not vary, you declare it as a constant by using the Java reserve word final, and, by convention only, making the identifier to be all caps.

For example:

int numberOfStudents = 21; //a variable, which can change
final int ARRAYLENGTH = 234; //a constant, which cannot change