Variables are memory locations used to store values. They can be visualized as storage bins as
shown
in Figure 2.1. Each memory location has a unique memory address. Each address can store one item
at
a time. Before a memory space can be used it needs to be reserved.
Variable declaration is reserving a memory location for storing values. It is done by writing a
statement containing a data type, name of the variable and (optional) initial value. The name
allows
the programmer to refer to the memory location later in the program using a descriptive word,
instead of the memory address.
Syntax:
dataType variableName [= initialValue];
Example 1: declare a variable of type integer with the name num
int num;
Example 2: declare an integer variable count and initialise it to zero
int count;
count = 0;
Note that the above two lines of code can be combined
into
1 as:
int count = 0;
When the above two examples are compiled, the data stored in memory is dipicted in Figure 2.2.
Data Types
The data type indicates what type of values a memory location will store (e.g., number, text or
address of another variable).
Two fundamental data types groups are:
Built-in (primitive type): Part of the compiler
Class: Created by the programmer
Two types of memory locations can be declared: variables and named constants:
Variables are memory locations whose values can change during runtime (when
the
program is running). The initial value is optional but recommended. If a variable is not
initialized, it contains the previous value of that memory location, which may be the wrong
type
(called a garbage value).
short and int types usually initialized to 0
float and double types usually initialized to 0.0
string types usually initialized to empty string (“”)
bool types initialized to either true or false
Named constants are memory locations whose values cannot change during
program
execution. It should be initialized with the value they will hold for the duration of the
program. The final keyword indicates that the memory location is a named
constant (value
cannot
be changed during runtime). The initialization is mandatory. A Literal is
an
actual value.
Syntax:
final dataType constantName = value;
Example:
final double PI = 3.14;
final double MIN_HEIGHT = 100.5;
final bool PAID = true;
final char NO = ‘N’;
final string COMPANY_NAME = “XYZ Company Ltd”;
Advantages of using constants:
Make the program more self-documenting (meaningful words in place of numbers)
Value cannot be inadvertently changed during runtime
Typing a name is less error-prone than a long number
Mistyping a constant’s name will trigger a compiler error; mistyping a number will
not
If the constant needs to be changed when modifying the program, it only needs to be
changed at one place
Java has eight basic data types - byte, short, int, long, char, float, double, and boolean:
byte - The byte data type can be useful for saving memory in large arrays, where the memory
savings actually matters.
bool - data type stores Boolean values (true and false)
short and int types - store integers (numbers without a decimal place). Differences are the
range of values and memory used (int is greater than short)
float and double types - store real numbers (numbers with a decimal place). Differences are
range of values, precision, and memory used (double has the greater of each)
char type - stores characters (letter, symbol, or number that will not be used in a
calculation). Only one character stored at a time
string data type - is a user-defined data type (defined with a class, or group of
instructions). Can store zero or more characters
Variable Names
The name chosen for the variable should be short (easy to remember) and descriptive
(understandable). Short names help the programmers easy to remember and result in more concise
code. Descriptive names make it easier to understand their purpose as well.
Rules for naming variables in python are:
Name can only begin with a letter or an underscore (_)
It can contain only letters, numbers, and the underscore character. No punctuation marks,
spaces, or other special characters (such as $ or %) are allowed
Cannot be a keyword (a word that has special meaning in Python)
Names are case-sensitive. Example: discount is different from DISCOUNT and Discount
Good Programming Practice for naming variables:
Use uppercase letters for named constants and lowercase for variables.
Example: PI (constant), radius (variable)
If constants contain more than one word, separate words with underscores. Example: TAX_RATE
If variables contain more than one word, capitalize the first letter of each word after the
first (called camel case). Example: adjustedGrossIncome