Why doesn’t Java support structure? (just out of curiosity)
I know you can use the public domain or some other solution Maybe you don't need them at all But out of curiosity, why did sun leave the building
Solution
Although Java can support any number of types, the runtime only supports some variable types: int, long, float, double and reference; In addition, the runtime only recognizes some object types: byte [], char [], short [], int [], long [], float [], double [], reference [], and non array objects The system will record the class type for each reference variable or array instance, and the runtime will perform some checks, such as ensuring that the references stored in the array are compatible with the array type, but such behavior only treats the object type as "data"
I disagree that the existence of classes eliminates the need for structures, because structures have fundamentally different semantics from class objects On the other hand, from the perspective of runtime system design, adding structures will make the type system very complex Without a structure, the type system requires only eight array types Adding structures to the type system will require the type system to recognize any number of different variable types and array types Such recognition is useful, but sun doesn't think it's worth the complexity
Considering the limitations of Java runtime and type system, I personally think it should contain limited forms of aggregate types Most of them are handled by the language compiler, but some functions are required to run at run time Whereas the statement
aggregate TimedNamedPoint { int x,y; long startTime; String name; }
Field declarations such as timednamedpoint TPT; Four variables are created: TPT x. TPT of type int y. TPT of type long TPT of type starttime and string name. Declaring parameters of this type behaves similarly
To make these types useful, the runtime needs some small additions: it is necessary to allow functions to leave multiple values on the stack when they return, rather than simply using the return value of one of the five main types In addition, it is necessary to have a way to store many things in the array Although this can be achieved by creating something declared as timednamedpoint [12], it is actually an object [4], which will be initialized to identify two instances of int [12], long [12] and string [12], It's better to have a method. Through this method, the code can construct a separate array instance, which can accommodate 24 int type values, 12 long type values and 12 string type values
Personally, I think for things like point, the semantics of simple aggregation will be clearer than classes In addition, the lack of aggregation often makes it impractical to have methods that can return more than one information at the same time In many cases, it is possible for the method to calculate and report the sine and cosine of the incoming angle at the same time, which requires much less work than calculating the two separately, but having to construct the sineandcosinresult object instance will deny any speed advantage The execution model does not need to be changed too much to allow the method to leave two floating-point values on the evaluation stack when it returns, but such things are not supported at present