Although Java can arbitrarily support many types of classes, Runtime only supports a few types of variables: int, long, float, double, and reference; In addition, Runtime recognizes only a few types of objects: byte [], char [], short [], int [], long [], float [], double [], reference [] and an object without an array. The system will record the type a class for each reference variable or instance of the array, and Runtime will perform certain checks, such as checking that the reference stored in the array is compatible with the type of the array, but this behavior simply treats the types of objects as "data."
I do not agree with the statement that the existence of classes eliminates the need for structures, because structures have semantics that are fundamentally different from class objects. On the other hand, in terms of the design of the Runtime system, adding structures greatly complicates the type system. In the absence of structures, a type system requires only eight types of arrays. Adding structures to the type system will require the type system to recognize an arbitrary number of different types of variables and array types. Such recognition is useful, but Sun believes that it is not a difficult task.
Given the limitations that the Java Runtime and type system works with, I personally believe that it should include a limited form of the aggregate type. Most of this will be handled by the language compiler, but it will take several functions in Runtime to complete this work. Given the declaration
aggregate TimedNamedPoint { int x,y; long startTime; String name; }
declaring a field of type TimedNamedPoint tpt;
will create four variables: tpt.x
, tpt.y
type int
, tpt.startTime
type long
and tpt.name
type String
. Declaring such a parameter will behave in a similar way.
In order for such types to be useful, Runtime will require a few minor additions: it is necessary that the functions leave several values ββon the stack when they are returned, instead of just having one return value from one of the five main types. In addition, it would be necessary to have a means of storing several kinds of things in an array. Although this can be achieved by creating something declared as TimedNamedPoint[12]
, in fact, it is Object[4]
that will be initialized to identify two instances of int[12]
, a long[12]
and a String[12]
, it would be better to have a way with which the code could build one instance of the array, it could contain 24 values ββof type int
, 12 of type long
and 12 of type String
.
Personally, I believe that for things like Point
, the semantics of a simple aggregate would be much cleaner than for a class. In addition, the lack of aggregates often makes it impossible to use a method that can return more than one kind of information at a time. There are many situations where it would be possible to simultaneously calculate a method and report the sine and cosine of angles with much less work than would be required to calculate both separately and to create an instance of the SineAndCosineResult
object, SineAndCosineResult
would deny any speed advantage that could be obtained thanks to to this. The execution model does not need to be significantly modified to allow the method to leave two floating point values ββin the evaluation stack when it returns, but this support is not currently supported.