Next Prev Up Contents


3 Types


3.1 - Numeric Types
3.1.1 - Integer Types
3.1.2 - Floating Point Types
3.1.3 - Character Types
3.2 - Boolean Types
3.3 - Arrays
3.3.1 - Array Detail
Every variable and every expression has a type. Type determines the allowable range of values a variable can hold, allowable operations on those values, and the meanings of the operations. Built-in types are provided by the Java language. Programmers can compose new types using the class and interface mechanisms (see "Classes" on page 11 and "Interfaces" on page 23).

The Java language has two kinds of types: simple and composite. Simple types are those that cannot be broken down; they are atomic. The integer, floating point, boolean, and character types are all simple types. Composite types are built on simple types. The language has three kinds of composite types: arrays, classes, and interfaces. Simple types and arrays are discussed in this section.


3.1 Numeric Types


3.1.1 Integer Types

Integers are similar to those in C and C++, with two exceptions: all integer types are machine independent, and some of the traditional definitions have been changed to reflect changes in the world since C was introduced. The four integer types have widths of 8, 16, 32, and 64 bits and are signed.

A variable's type does not directly affect its storage allocation. Type only determines a variable's arithmetic properties and legal range of values. If a value is assigned to a variable that is outside the legal range of the variable, the value is reduced modulo the range.


3.1.2 Floating Point Types

The float keyword denotes single precision (32 bit); double denotes double precision (64 bit). The result of a binary operator on two float operands is a float. If either operand is a double, the result is a double.

Floating point arithmetic and data formats are defined by IEEE 754. See "Appendix: Floating Point" on page 37 for details on the floating point implementation.


3.1.3 Character Types

The language uses the Unicode character set throughout. Consequently the char data type is defined as a 16-bit unsigned integer.


3.2 Boolean Types

The boolean type is used for variables that can be either true or false, and for methods that return true and false values. It's also the type that is returned by the relational operators (e.g., ">=").

Boolean values are not numbers and cannot be converted into numbers by casting.


3.3 Arrays

Arrays in the language are first class objects. They replace pointer arithmetic. All objects (including arrays) are referred to by pointers that cannot be damaged by being manipulated as numbers. Arrays are created using the new operator:

    char s[] = new char[30];
The first element of an array is at index 0 (zero). Specifying dimensions in the declarations is not allowed. Every allocation of an array must be explicit--use new every time:

    int i[] = new int[3];
The language does not support multi-dimensional arrays. Instead, programmers can create arrays of arrays:

   int i[][] = new int[3][4];
At least one dimension must be specified but other dimensions can be explicitly allocated by a program at a later time. For example:

   int i[][] = new int[3][];
is a legal declaration.

Subscripts are checked to make sure they're valid:

   int a[] = new int[10];
   a[5] = 1;
   a[1] = a[0] + a[2];
   a[-1]	 = 4;         // Throws an ArrayIndexOutOfBoundsException
                        // at runtime
   a[10] = 2;           // Throws an ArrayIndexOutOfBoundsException
                        // at runtime
Array dimensions must be integer expressions:

   int n;
   ...
   float arr[] = new float[n + 1];
The length of any array can be found by using .length:

   int a[][] = new int[10][3];
   println(a.length);           // prints 10
   println(a[0].length);        // prints 3

3.3.1 Array Detail

Arrays are instances of subclasses of class Object. In the class hierarchy there is a class named Array, which has one instance variable, "length". For each primitive type there is a corresponding subclass of Array. Similarly, for all classes a corresponding subclass of Array implicitly exists. For example:

   new Thread[n]
creates an instance of Thread[]. If class A is a superclass of class B (i.e., B extends A) then A[] is a superclass of B[] (see the diagram below).

Hence, you can assign an array to an Object:

   Object o;
   int a[] = new int[10];
   o = a;
and you can cast an Object to an array:

   a = (int[])o;	
Array classes cannot be explicitly subclassed.


Next Prev Up Contents

The Java Language Specification

Generated with CERN WebMaker