What are they
A variable refers to a place in memory for a value (data) used in an application.
- By using the variable's name the value it refers to can be changed.
Imagine a variable named counter that keeps track of an event. Each time the event happens counter's value increments or decrements. As counter's value changes so does the value held in memory.
So, if you need somewhere to hold and remember a value, you hold it in a variable.
Where are they
Variables are found in two different places -
- inside a class
- inside a method
To understand the difference between these two, you should know the following:
Variable Scope
Scope (or visibility) depends on where you've declared the variable -
- If declared inside a method, they're only visible to that method
- If declared as
private at the top of a Class (outside any method) they can be seen by any method in that Class
- If declared as
public at the top of a Class (outside any method) they can be seen by all class's methods within that package
Variable Lifespan
Life Span (or Extent) depends on where you've declared the variable -
- If declared inside a method, the variable lives in memory as long as the method is running
- If declared at the top of the Class
private or public, it lives as long as an object of the class exists.
- If declared as
static at the top of the Class, it lives as long as the program is running.
What to call each
Depending on where they are declared variables are referred to as -
- Local - if declared inside a method
- Instance - if declared inside a class (outside any methods) using the keyword
private or public
- Static - if declared inside a class (outside any methods) using the keyword
static
- Variables declared at class level are often called fields
Naming Variables
You choose the names of your variables. There are some naming conventions that you should ALWAYS follow.
- Use a name that someone besides you will readily understand
- Use lower case letters
- If you choose two or more words join them - no spaces allowed
- When joining words the second and further words should start with a capital. This is called camelCase
Naming variables
- A good name -
dateOfBirth
- A bad name -
dob
- A good name -
uniqueID
- A bad name -
uid
- Although the maximum recommended length is about 32 characters, be reasonable - you shouldn't have any names that long.
Data Types
- In Java all variables must have a data type.
- It's a classification for a value used in a program.
- The data type assigned determines how the value can be used.
- You decide what data type to assign.
Here are the primitive ones-
Examples...
Primitive data
Primitive data types:
short 16-bit whole number
int 32-bit whole number
long 64-bit whole number
float 32-bit floating point
double 64-bit floating point
byte from -128 to 127
boolean either true or false
char holds a single character
Other data types...
More data types
boolean either true or false
char it holds a single character
Note 1 - These are all referred to as primitive data types as opposed to being objects. That basically means they are simple data.
Note 2 - String (which holds multiple characters together) is an object.
Object Types
An object is different from a variable. It's more complex and has properties and methods. But like a variable, to use an object you must first declare it.
The data type of an object is it's name. So a String object has a data type of String and the word 'String' is used in the declaration.
String FirstName = new String();
For more on object data types see the Objects section.
Declaration
Declaration is setting the characteristics of a variable. A declaration includes -
- It's scope (if declared at class level)
- Data Type - this gives the data it's meaning
- Name - a word that describes the value's use
- An initial value - this is generally optional but sometimes you'll need to provide one. It's called initialisation.
See the examples page
Variable types
boolean data type
boolean choice = false;
int
int amount;
byte
byte b = 89;
char
char c = 'f';
Notice something?
Each statement ends with a semi-colon ;
More...
Variable types
Integer
int counter = 0;
A decimal - double
double interestRate;
Another decimal - short
short angleDegree;
Another decimal - float
float temperature = 32.5;
Boolean - true or false
boolean connected = false;
A byte
byte arrayCount = 4;
A long number
long bigNumber;
Quiz
Question 1
Question 2
- An application is written to calculate someone's age by subtracting the current year from their birth year. What data type would you choose when declaring the variable that would hold the age value?
- String
- boolean
- int
- number
Question 3
Question 4
- An application uses a boolean variable to keep track of whether an item is signed out or not. All items start as not being signed out and the variable is named
signedOut.
Which of these statements is the correct way to initialise the variable?
boolean signedOut = false;
boolean signedOut == false;
Question 5
Question 6
Question 7
End of Quiz
Well done.