Contents | Prev | Next | The T Language Specification, Version 2 Spring 2006 |
CHAPTER 2
The T programming language is a strongly typed language, which means that every variable and every expression has a type that is known at compile time. Types limit the values that a variable can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of the operations. Strong typing helps detect errors at compile time.
The types of the T programming language are divided into two
categories: primitive types and reference types. There is only one
primitive type, the integer type int
.
The reference types are class types and array types. There is also a
special null type. An object is a dynamically created instance of a
class type or a dynamically created array. The values of a reference
type are references to objects. All objects, including arrays, support
the methods of class Object
. Names of types are used in declarations, class instance creation expressions and array creation expressions.
A variable is a storage location. A variable of a primitive type
always holds a value of that exact type. A variable of a class type T can hold a null reference or a reference to an instance of class T or of any class that is a subclass of T. If T is a primitive type, then a variable of type "array of T" can hold a null reference or a reference to any array of type "array of T"; if T is a reference type, then a variable of type "array of T" can hold a null reference or a reference to any array of type "array of S" such that type S is assignable to type T. A variable of type Object
can hold a null reference or a reference to any object, whether class or array.
There are two kinds of types in the T programming language: primitive types and reference types. There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values and reference values.
Type: PrimitiveType ReferenceType
There is also a special null type, the type of the expression null
,
which has no name. Because the null type has no name, it is impossible
to declare a variable of the null type. The
null reference is the only possible value of an expression of null
type. The null reference can always be converted to any reference type. In
practice, the programmer can ignore the null type and just pretend that null
is merely a special literal that can be of any reference type.
A primitive type is predefined by the T programming language and named by its reserved keyword. There is only one of these:
PrimitiveType:
int
Primitive values do not share state with other primitive values. A variable whose type is a primitive type always holds a primitive value of that same type. The value of a variable of primitive type can be changed only by assignment operations on that variable.
The values of the integer type are integers from -2147483648 to 2147483647, inclusive.
The T programming language provides a number of operators that act on integer values and all result in a value of type int
:
<
, >
, and ==
-
!
*
and /
+
and -
The built-in integer operators do not indicate overflow or underflow in any way.
There are two kinds of reference types: class types and array types.
ReferenceType:
ClassType
ArrayType
ClassType:
TypeName
ArrayType:
Type [ ]
The sample code:
class Point { int[] metrics; }
declares a class type Point
, and uses an array type int[]
(an array of int
) to declare the field metrics
of the class Point
.
An object is a class instance or an array.
The reference values (often just references) may be pointers to these objects, or a special null reference, which refers to no object.
A class instance is explicitly created by a class instance creation expression. An array is explicitly created by an array creation expression.
A new array object is implicitly created when an array initializer expression is evaluated; this can occur when a class is initialized, when a new instance of a class is created, or when a main block variable declaration statement is executed.
The operators on references to objects are:
==
There may be many references to the same object. Most objects have a state, stored in the fields of objects that are instances of classes or in the variables that are the components of an array object. If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the object, and then the altered state can be observed through the reference in the other variable.
The class Object
is a superclass of all other classes.
A variable of type Object
can hold a reference to any object, whether it is an instance of a class
or an array.
All class and array types inherit the only method of class Object
, which is summarized here:
int equals(Object obj) { . . . }
The equals
method defined by class Object
returns
1 if the two references refer to the same object and 0 otherwise.
This method can be overriden in subclasses to define
a notion of object equality, which is based on value, not reference, comparison.
The Object
class defines a constructor:
Object() { }Note that the body of the constructor is empty. Since Object is the primordial class and has no superclass, there is no call, either implicit or explicit, to the superclass constructor.
The Object
class also defines a destructor:
~Object() { }
As Object
is a superclass of every class, every time a class instance is deleted this destructor is ultimately called.
Two reference types are the same type if:
Types are used in declarations, in class instance creation expressions, and in array creation expressions.
A variable is a storage location and has an associated type, sometimes called its compile-time type, that is, either a primitive type or a reference type. A variable's value can be changed by an assignment and a variable may only be assigned a value that is assignment compatible with its type.
Compatibility of the value of a variable with its type is guaranteed by the design of the T programming language except in the case where an object has been deleted (in which case the value of the variable does not have a defined type). Otherwise, default values are compatible and all variable assignments are checked for assignment compatibility at compile time, run time, or both (reference types).
A variable of a primitive type always holds a value of that exact primitive type.
A variable of reference type can hold either of the following:
There are five kinds of variables:
Every variable in a program must have a value before its value is used:
int
, the default value is zero, that is, 0
.
null
.
In the T programming language, every variable and every expression has a type that can be determined at compile time. The type may be a primitive type or a reference type. Reference types are introduced by type declarations, which include class declarations.
Every object belongs to some particular class: the class that was mentioned in the creation expression that produced the object. This class is called the class of the object. (Arrays also have a class, as described at the end of this section.) An object is said to be an instance of its class and of all superclasses of its class.
Sometimes a variable or expression is said to have a "run-time
type". This refers to the class of the object referred to by the value
of the variable or expression at run time, assuming that the value is
not null
.
The compile time type of a variable is always declared, and the
compile time type of an expression can be deduced at compile time. The
compile time type limits the possible values that the variable can hold
or the expression can produce at run time. If a run-time value is a
reference that is not null
, it refers to an object or array that has a class, and that class will necessarily be compatible with the compile-time type.
Every array also has a class; this class is Array[component-type]
, where component-type can be either int
or the name of some reference type. This class name is not a valid
identifier, so conflict with any user-defined classes is impossible.
Contents | Prev | Next | The T Language Specification, Version 2 Spring 2006 |
Author(s): Tom Olson (§2.1-2.5)