Introduction
In computer programming, a variable is a symbolic name, given by the programmer, to a memory location in which data can be stored. A memory location is referenced by a variable name (e.g. price). The variable price will probably refer to a memory location with the value 8.90. The value stored in the memory location can dynamically change during the execution of the program.
Data types
Programs have to store and manipulate various types of data. Frequently used data types are numbers, single characters and texts. Java groups these data types in primitive and non-primitive data types.
Primitive data types
A primitive data type is defined by a reserved keyword in Java. Java supports the following primitive data types:
Data type | #bits | Range |
---|---|---|
byte | 8 | integer (-128 -> +127) |
short | 16 | integer (-32768 -> +32767) |
int | 32 | integer (-231 -> +231 – 1) |
long | 64 | integer (-263 -> +263 – 1) |
float | 32 | decimal (single precision) |
double | 64 | decimal (double precision) |
char | 16 | one character |
boolean | true or false |
Integer values (whole numbers without decimals) can be defined as byte, short, int or long. The data type chosen by the programmer depends on the size of the integer to be stored. A constant (also called a literal) is of type long if it ends with the letter L or l (e.g. 987650L).
Decimal values (fractional numbers with a decimal point) must be declared as float or double. A float data type has about 6 or 7 significant digits (number of decimals behind the decimal point). A double data type has a precision of about 15 decimal digits. A constant (literal) is of type float if it ends with the letter F or f (e.g. 1234.567F). A constant (literal) is of type double if it ends with the letter Dor d (e.g. 123456.7896D).
The char data type can contain one single character (e.g. ‘A’, ‘g’). A single character constant (literal) must always be placed between single quotes.
The boolean data type can only store the values true and false and will mainly be used to flag a condition.
Non-primitive data types
Non-primitive data types are objects based on classes. Primitive data types always start with a lowercase letter (e.g. int, long, float…). Non-primitive data type will always start with an uppercase letter (e.g. String, Array, …). A non-primitive data type will provide methods to perform certain operations on an object.
The String data type is an object based on the String class. A String object will be used to store a sequence of characters. String constants are always written between double quotes(e.g. “Brussels”) . The String class provides methods to determine the length of a string, to replace characters in a string, to create a substring…
Non-primitive data types will be covered in separate lessons.
Java Variables
Variable names
It is good practice to choose meaningful names for variables in a program. This makes the source code easier to read and understand. Since Java is case-sensitive a variable with the name Price does not refer to the same memory location as PRICE or price.
It is advisable to use all lowercase characters for the names of variables (e.g. price). However, if the name consists of several separate words, it is good practice to capitalize the first letter of subsequent words (e.g. newCost).
The following naming rules are important:
1. The name of a variable is case-sensitive. 2. A reserved word (e.g. float, public, static...) is not allowed to be used as a variable name. 3. The name of a variable must always begin with a letter. 4. The name of a variable may only contain letters, digits, dollar signs ($) or underscore characters (_). Other characters are not allowed.
Declaration of Variables
A variable must be declared before it can be used. Declaration is needed to reserve the appropriate memory space for the value to be stored. One memory location can hold only one value (number, character, boolean, etc.).
Variables can be declared as follows:
data type variable1, variable2...;
Examples: byte score; char letter; double price; int age, salary;
A variable can be initialised immediately during its declaration:
data item variable1 = literal;
Examples: byte score = 4; char letter = 'B'; double price = 3456.65D int age = 25; boolean sorted = false;