Software Sue

Link to Simply Sue
Link to Sustainability Sue
location: software/java/java variables Skip navigation : Home  

Java Variables


You store the information you use in programs in named pieces of memory called variables. Each variable can hold only one type of data. Different data types include integers, decimals and strings. The compiler will tell you if you try to use a method designed for one data type on another. You must specify the name and type of a variable in a declaration before you use it.

Variable names

The name of each variable or anything else in Java is called an identifier. The rules for identifiers are:

Note that Java is case sensitive. So 'day' and 'Day' are different. It is conventional but not mandatory to start variable names with a lower-case letter, and to capitalise any other words held within the name, for example dayMusicDied. Try and make the names meaningful.

Data Types

Variables reference either objects or one of the eight basic data types. They can hold integer or floating point numeric values, a single Unicode character, or the logical value true or false. They are all Java keywords. They are:

Data TypeDetails
bytebyte is a data type consisting of an 8 bit two's complement integer with values between -128 and 127.
charfloat is a data type consisting of a 16 bit Unicode character. For alphanumerics, these are the same as ASCII with the high byte set to 0.
doublefloat is a data type consisting of 32 bit single precision floating point number.
floatfloat is a data type consisting of 64 bit double precision floating point number.
intint is a data type consisting of a 32 bit two's complement integer with values between -2,147,483,648 and 2,147,483,647.
longlong is a data type consisting of a 64 bit two's complement integer with a huge range of values.
shortshort is a data type consisting of a 16 bit two's complement integer with values between -32,768 and 32,767.
voidA return type meaning that no information is returned by a method.

Any value, such as 1, 3.14, or "Hello World!" is referred to as a literal in Java.

Integer data types

These are byte, int, long and short. The default for integer literals is int, which is sufficient for all but the largest numbers. Using byte and short instead saves a little memory, but not a significant amount unless you have a lot of them. The minus sign in front of a negative integer is called the unary -. There is also a unary +, but it is optional. To define an integer of type long, append an L to the value, for example 1L, -234L, 56789L (avoid lower case L as it looks too similar to 1 (one)). Any literal that you specify as int but which is too large will automatically be stored as long, in spite of not having an L.

You can specify integer values in hexadecimal (base 16) by putting 0x in front of them. Hexadecimal numbers use 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F (where F stands for decimal 15, for example). Other examples are: 0x10 = 16, 0x100 = 256, 0xFF = 255. Hexadecimal numbers are most often used to define a particular sequence of binary digits, or bit pattern. The simplest integer variable declaration takes the form:

long bigInteger;

bigInteger will be allocated 8 bytes of memory at compilation time. Its value will default to 0. Initialising a variable is recommended. The format for this is:

long myLongInteger = 8181818L;
int myInteger = 2147483648;
byte myByteInteger = 127;
short myShortInteger = 32767;

If you were to forget the L on the long declaration, the literal will be stored as type int and then converted to long before being stored, which involves an extra step.

There are a lot of places within a Java program where you can declare variables, but they do have to be declared before they can be used, or you will get a compile error. Where you declare variables affects which parts of the program can access them.

You can declare more than one variable in the same sentence by separating them by a comma, for example:

long bigInteger = 818L, biggerInteger = 81818L, biggestInteger = 8181818L;

Only do so if it makes sense and is readable, for example declaring an x,y coordinate pair on the same line.

Floating point data types

When they are not integers (whole numbers), numbers are held in floating point data types. Floating-point numbers have a fixed number of digits of accuracy (just one, the 2, in the following example) but with a very wide range of values, because the decimal point can 'float'. For instance, 0.0002, 200.0 and 2000000.0 are written as 2x10-5, 2x102, and 2x106 respectively. Java's two floating point data types, float and double, offer different number-of-digits precision, in line with the IEEE 754 standard.

Data TypeDetails
floatHolds values from -3.4E38 (-3.4x1038) to +3.4E38 (+3.4x1038) in 4 bytes in memory. Values are represented with approximately 7 digits accuracy.
doubleHolds values from -1.7E308 (-1.7x10308) to +1.7E308 (+1.7x10308) in 8 bytes in memory. Values are represented with approximately 15 digits accuracy. The smallest non-zero value that you can have is roughly (4.9x10-324).

Java treats floating point literals as type double by default. If you want them to have type float, append an 'f' or 'F', for example 2.3F, 456.789f.

You can write small floating-point values with an exponent - a decimal value multiplied by a power of 10. For example, 1234000 (1.234x106) can be written as 1.234E6 or 1.234e6. This is particularly useful with very small or very large floating-point values.

The format for declaring floating point variables is:

double myDouble = 1.234E5;
float myFloat = 6E-78F;

More than one floating-point variable can be declared in a single statement:

float roomHeight = 10.5F, roomLength = 25.6F, roomWidth = 18.0F;