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
 
  9.2E18 is the computer's way of representing 9.2 x 1018

  The E means times 10 to the power of.

  0.0004256 = 4.256 x 10 -4 and is written in computer notation as 4.256E-04

Mostly you use int or long for integers or double for your numeric data types, depending on whether you need whole numbers or a fractional part.

A char literal is surrounded by apostrophes on each side eg 'A'  'm' 

Control characters are represented by several characters inside apostrophes.  eg '\n' represents a new line character.

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;

 
  •  A primitive data value uses a small, fixed number of bytes.
  •  You can not create new primitive data types, but you can create objects. The data type of an object is called its class.
  •  You can create new classes to meet the particular needs of a program.

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

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

 

Work through the code in the above sample until you understand how it works.  Note how the output is a combination of character strings in double quotation marks and values of variables outside quote marks.  To use the value stored in a variable, just use the name of the variable. Do not enclose it in quote marks.

1. Adjust the code so that  it will calculate area and perimeter of a rectangle with sides 44x87metres.

2. Write a new program that will calculate the area and perimeter of a circle from a given radius.