Next page | Contents page |

Primitive data types in Java

There are just 8 primitive data types. We have already met int but there are 3 other types for whole numbers. They differ in the range of values they can hold. All are signed (unlike C/C++ there are no unsigned versions). They all use their top bit to indicate sign:

To force a number to be treated as long rather than the default int append the letter L to it. Eg,

long msPerYear = 365L * 24L * 60L * 60L * 1000L; // milliseconds per year

In this example failure to use the letter L results in the expression being evaluated as an int and the value overflows the maximum possible value for an int.

There are 2 data types for floating point (ie, non-integral) numbers, eg -1.234E-5, where E means "10 to the power of" - the same as in most programming languages:

The default treatment for a floating-point number is double. To force a number to be treated as a float you must put the letter F after it. Eg,

float pi = 3.141596F;

There are 2 other primitive types:

That is all of the so-called primitive data types. Other data types are classes, interfaces and enums, of which there can be as many as you care to define. We will come to those later.

Next page | Contents page |