Java variables
This lesson focuses on:
- What is a variable?
- Declaring variables
- Naming variables
- Printing variables
What is a variable?
A variable is a container which holds information in a computer's memory. The value of a variable can change all throughout a program.
Declaring variables
In Java, different variables store different types of data. The type of data that is stored by a variable is signified with a data type.
Java data types:
| Data type | Type of data it stores | Size in memory |
|---|---|---|
| boolean | true/false value | 1 bit |
| byte | byte size integer | 8 bits |
| char | a single character | 16 bits |
| double | double precision floating point decimal number | 64 bits |
| float | single precision floating point decimal number | 32 bits |
| int | a whole number | 32 bits |
| long | a whole number (used for long numbers) | 64 bits |
| short | a whole number (used for short numbers) | 16 bits |
A variable is declared with the data type of the data it will store.
Syntax:
dataType varName;
Example:
char aCharacter; int aNumber;
In the above example, two variables are declared. The first variable is used to store a single character and is therefore of data type char. The second variable is used to store a whole number and is therefore of data type int. You can assign a value to a variable at the same time that it is declared. This process is known as initialization.
Example of initializing a variable:
char aCharacter = 'a'; int aNumber = 10;
In the above example a character variable named aCharacter is initialized with the value 'a' and a numeric variable is initialized with the value 10.
Example of declaring a variable and then giving it a value:
char aCharacter aCharacter = 'a'; int aNumber aNumber = 10;
In the above example a character variable named aCharacter is declared. On the next line this variable is assigned the value 'a' and a numeric variable named aNumber is declared and on the next line it is assigned the value 10.
NOTE: A variable must be declared with a data type. If you do not specify a data type for a variable, an error will be generated! Furthermore, The data type of a variable should be used only once with the variable name - during declaration. After a variable has been declared or initialized, you can refer to it by its name without the data type.
Naming variables
When naming variables, several rules should be considered. These rules will make variable declaration easier, as well as clear up ambiguity and possible errors in code.
-
Make sure that the variable name is descriptive
If you do not give a variable a descriptive name, it will be hard to understand what the variable refers to. For example, if you wanted to create a variable which would hold a value describing the amount of chairs in a room, which variable name would be more appropriate - numChairs or a? The better choice for the variable name would be numChairs because it is more descriptive.
-
Make sure the variable name is of appropriate length
Make sure the variable name is long enough to be descriptive, but not too long.
-
Do not use spaces in variable names.
This is a rule that must be followed in Java because Java does not allow spaces in variable names, doing so will generate an error!
-
Do not use special symbols in variable names such as !@#%^&*
As is the rules with spaces, special symbols are not allowed in variable names, and using special symbols in variable names will generate an error. There is however one special symbol that can be used in variable names, and that symbol is the undersocre ( _ ) symbol. Variable names can only contain letters, numbers and the underscore ( _ ) symbol.
-
Variable names can not start with an integer
While an integers can be used in variable names, it cannot be the first character in a variables name. Variable names can only start with a letter or the underscore ( _ ) symbol.
-
Distinguish between uppercase and lowercase
Java is a case sensitive language which means that the variables varOne, VarOne, and VARONE are three separate variables!
-
When referring to existing variables, be careful about spelling
If you try to reference an existing variable and make a spelling mistake, an error will be generated!
Printing variables
Variables are printed by including the variable name in a System.out.print() or System.out.println() method. When printing the value of a variable, the variable name should NOT be included in double quotes.
Example:
class PrintText{
public static void main(String[] args){
//declare some variables
byte aByte = -10;
int aNumber = 10;
char aChar = 'b';
boolean isBoolean = true;
//print the variables
System.out.println(aByte);
System.out.println(aNumber);
System.out.println(aChar);
System.out.println(isBoolean);
}
}
Output:
-10
10
b
true
You can also print variables together with regular text. To do this, use the + symbol to join the text and variable values together.
Example:
class PrintText{
public static void main(String[] args){
//declare some variables
byte aByte = -10;
int aNumber = 10;
char aChar = 'b';
boolean isBoolean = true;
//print the variables
System.out.println("aByte = " + aByte);
System.out.println("aNumber = " + aNumber);
System.out.println("aChar = " + aChar);
System.out.println("Is the isBoolean
variable a boolean variable? " + isBoolean);
}
}
Output:
aByte = -10
aNumber = 10
aChar = b
Is the isBoolean variable a boolean variable? true
Output:
Here is some more text




