Variable Types, Classes, and Interfaces

In the Expression Language, every variable and every expression has a type that can be determined at parse time. The type may be a primitive type or a reference type. Reference types include class types and interface types. Often the term type refers to either a class or an interface.

Every object belongs to some particular class: the class that was mentioned in the creation expression that produced the object, the class whose Class object was used to invoke a reflective method to produce the object, or the String class for objects implicitly created by the string concatenation operator (+), or the Document class for objects implicitly created by the document concatenation operator (+) or the time of day, time of week, day of week document operator (||), or the Prompt class for objects implicitly created by the prompt concatenation operator (+) or the prompt escalation, time of day, time of week, day of week or random prompt operator (||) or the prompt substitution operator (|||), or the Grammar class for objects implicitly created by the grammar compound operator (||). 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 super classes 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 parse time type of a variable is always declared, and the parse time type of an expression can be deduced at parse time. The parse 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 parse-time type.

Even though a variable or expression may have a parse-time type that is an interface type, there are no instances of interfaces. A variable or expression whose type is an interface type can reference any object whose class implements that interface.

Here is an example of creating new objects and of the distinction between the type of a variable and the class of an object:


					{
							java.util.ArrayList alist = new java.util.ArrayList();
							java.util.Vector vector = new java.util.Vector();
							java.util.List list;

							list = alist;
							list = vector;	
					}

In this example:

  • The local variable list has as its type the interface java.util.List, so it can hold a reference to any object whose class implements java.util.List; specifically it can hold a reference to either java.util.ArrayList or java.util.Vector.

  • Note that an expression such as "new java.util.List()" is not valid because it is not possible to create an instance of an interface, only of a class.

Every array also has a class; the method getClass, when invoked for an array object, will return a class object (of class Class) that represents the class of the array.

The classes for arrays have strange names that are not valid identifiers; for example, the class for an array of int components has the name "[I" and so the value of the expression:

			new int[10].getClass().getName()

is the string "[I"; see the specification of Class.getName for details.