Variables
We use variables to store data. Think of a variable as a container for storing a data object. Variable names must be legal Java identifiers (see below). Java is what is known as a strongly typed language. That means that every variable must have a declared data type. There are 8 primitive data types you can use. Four integer types, two float types, one character type and one boolean type.
| Groups | Variable Type | Storage | Approx Range |
| Integer | int | 32 bits | +/- 2 billion |
| short | 16 bits | -32,768 to +32,767 | |
| long | 64 bits | +/- 9.2 E18 | |
| byte | 8 bits | -129 to 127 | |
| Floating Point | float | 32 bits | -3.4E+38 to +3.4E+38 |
| double | 64 bits | -1.7E+308 to 1.7E+308 | |
| Character | char | 16 bits | 65,536 |
| Boolean | boolean | 1 bit | true and false |
![]() |
|
Integer types have no fractional part; floating point types have a fractional part. On paper, integers have no decimal point, and floating point types do.
You must declare every variable in a Java program by specifying its name and type. Here is an example of declaring two variables of type int:
int number1, number2;
You can also give a variable its initial value in a declaration:
float length=9.44;
or a combination
int number1=367, number2, number3;
![]() |
|
Strings
Java also uses strings. A string is a class not a primitive data type. The Java string class is part of the java.lang library that you can access through any Java program. You can create variables in the String class just as if they were primitive data types, but because String is a class not a data type it begins with a capital letter. All primitive data types begin with a lower case letter.
A string object represents a sequence of characters. You declare and initialise Strings similarly to other data types, but the string is enclosed in double quotation marks:
String userName = "Fred"
The Java String class contains methods that enable you to manipulate and convert strings.
Identifiers
Identifiers are the names of program elements, such as variables, methods, constants, classes etc. There are strict rules for naming identifiers
-
You can use any letters or digits from the Unicode character set as well as $ and _
-
You are not allowed to use spaces.
-
Because Java is case sensitive, userName and Username represent different identifiers.
-
An identifier cannot start with a digit.
-
An identifier cannot be one of Java's special keywords, such as byte, break, if, for, return etc These are words reserved for the programming language.
Here are some examples of legal and illegal identifiers:
| Legal | Illegal |
| x_234 | 2user |
| userName_One | User Name |
| $$23 | !!23 |
| address_and_postcode | boolean |
You should choose descriptive identifier names so that your code is easy to understand.
Scope of Variables
When you declare a variable, you can only use it within the set of curly braces { } in which it is declared. This is deliberately done to prevent misuse of variables and potential errors.
Constants
Constants are values that don't change. They are variables that are defined with the keywords static and final. Constants must be declared at the class level and cannot be declared within a method. Constant names usually begin with a capital letter. If you begin variables with a lower case letter, then you can easily see which are constants and which are variables.
Example Program
Here is a sample program that takes set values for length and width and calculates area and perimeter for a rectangle:
/*Simple Java program to calculate area and perimeter
of a rectangle given a fixed length and width.
Created by M Meijers March 2005 */
class Rectangles
{
public static void main(String[] args)
{
int length=3, width=5, area, perimeter; //declare variables
area=length*width; //calculate area
perimeter=2*(length+width); //calculate perimeter
System.out.println("Length " + length + "cm, Width " +width+"cm");
System.out.println("The area is " + area + " square cm.");
System.out.println("The perimeter is " + perimeter+" cm.");
}
}
|
-
Copy and paste the code into Notepad and save it as Rectangles.java
-
In a command window, change directory until you are in the folder where you saved Rectangles.java
-
Compile it by typing javac Rectangles.java
-
Run it by typing java Rectangles
![]() |
|



