Using data types and variables
Variables can be thought of as ‘containers’ for data. Each variable has to have a unique name, so that you can refer back to it in the program.
By using variables, we can refer to values that haven’t yet been defined. For example, we can refer to the speed of the ball in different parts of the game code, but calculate the ball speed later based on different inputs.
How a variable is named is mainly up to you, but it’s a good idea to use names that reflect the purpose, for example
mBall refers to the ball in the game code.
However, there are some rules for naming variables in Java that have to be followed:
- The name should begin with a letter, the dollar sign “$”, or an underscore “_”.
- The name can’t be a ‘reserved word’. Reserved words are used by the Java language to refer to specific things.
For a full guide to naming variables in Java, see the variables documentation on the Oracle website.
When creating a variable, we also need to declare the data type it contains. This is because the program will use different types of data in different ways.
Programming languages define data types differently. For example, almost all languages differentiate between ‘integers’ (or whole numbers, eg 12), ‘non-integers’ (numbers with decimals, eg 0.24), and ‘characters’ (letters of the alphabet or words). But, in Java there are also several ‘primitive’ data types:
char– a single 16-bit Unicode character, such as a letter, decimal or punctuation symbol.
boolean– can have only two possible values: true (1) or false (0). This data type is useful in conditional statements, which we will cover in more detail in week 3.
byte- has a minimum value of -128 and a maximum value of 127 (inclusive).
short– has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
int:– has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).
long– has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
float– a floating point number with 32-bits of precision
double– this is a double precision floating point number.
The data types that hold numeric values are:
double. The difference between them is the size and precision of the value they contain.
Multiple characters or ‘strings’ (eg words and sentences) are also supported in Java through the use of
Declaring a variable
Before using a variable in a Java program it has to be declared. Think of it as putting a label on your container that describes the content.
If you want to declare two integer type variables with the names
MyInteger in Java it will be written as:
int myInteger; int MyInteger;
This declaration comprises of 3 parts – first the data type
int is declared, then the variable name
myInteger and finally the statement is concluded with a semicolon
;. The Java syntax (or arrangement) requires us to end each ‘statement’ or block of instructions with a semicolon.
Java variable names are case sensitive, so
myInteger are considered to be two different variables. To avoid confusion, it is better practice to choose names that are more distinct.
In this case the variable names indicate their use:
float mBallSpeedX; float mBallSpeedY;
mBallSpeedX holds the speed of mBall in the horizontal direction (or X axis), while
mBallSpeedY holds the speed of mBall in the vertical direction (or Y axis).
It is also possible to assign values to variables in the same statement as the declaration. For example;
float mBallX = 0; float mBallY = 0;
This tells the program that the variable with the name mBallX contains floating point data and that the value of this is 0 (it is situated at 0 on both the X and Y axis).
Variables have a life time, also referred to as scope. The variables declared inside methods only ‘live’ inside the methods, whereas the variables declared outside the methods (the ones we start with a lower case m) can be accessed from all of the methods, because they ‘live’ inside TheGame class. We’ll be covering methods (also called functions) in more detail in Week 6.
Now you have been introduced to variables and data types and learnt how to declare a variable and assign it values, you can test your knowledge in the quiz in the next step.
© University of Reading