Example code detailing difference in typing between Ada and Java [closed]

人走茶凉 提交于 2020-01-29 23:30:28

问题


I'm trying to think of a good example detailing the difference between Ada's strict typing and Java's strong typing. Does anyone have a good example for identifying these differences in type conversion using integer based values?


回答1:


This may prove helpful, from the Ada perspective.

There are two basic forms of a type declaration : the new type, and the subtype.

A new type is regarded as a completely different type from any previous type, even if it has the same range of values. Objects of the new type can only be assigned to variables of the new type, and so on.

A subtype is derived from an existing type, shares a subset of its values, and is regarded as being essentially the same type as far as assignment is concerned.

The art of using the type system takes a little practice, and this – choosing between types and subtypes - is one area where it matters. It makes the difference between frustratedly fighting the type system, and having programs fit together simply and easily.

  • Declare a new type where its values denote something new, something you won't need or want to mix with other quantities.
  • Declare a subtype where its values are related to existing things.

An example : imagine automating a building, including its lift system :

type Floor is new Integer range -3 ..135;

There are a couple of underground garages, a basement, 0 is the ground floor (for an European rather than American building!) and 135 floors above it, making it one floor taller than the Towering Inferno.

This can safely be a new type because there is little danger of confusing a Floor variable with anything else, and very little need to mathematically combine a Floor with any other quantity. Lifts need to go there, and that's about it. In this case, having Floor as a new type can catch some errors, but the added safety is worthwhile without the prospect of pain.

Using types or subtypes as array indexes and loop boundaries is a good way to make out-of-bounds accesses rather unlikely.

Array(Floor) of ...
for f in Floor loop ...

If you do need to assign from an integer variable to a Floor, a type conversion signals to both the compiler and anyone reading the code that this as intentional.

subtype Population is Natural range 0 .. 10000;

We must know how many people occupy the building, for security, fire safety, heating or cooling requirements, and other purposes. Its range has hard limitations at both ends : for this building, let's assume more than 10000 occupants have been prohibited by fire safety regulations.

The base type is Natural (itself a subtype of Integer) rather than Integer, because the population can never be less than zero.

But making Population a new type might cause endless type conversion issues. Making it a subtype makes it easier to use Population in heat load calculations, lift scheduling strategies, water use predictions, for a few examples.

The fact of its being a subtype rather than just Integer still provides useful forms of protection; while integers can be assigned to Population variables and mixed with them in expressions, any attempt to assign out-of-range values will be flagged as errors. If they cannot be detected at compile time, they will raise an exception at runtime. And so will the 10001th person attempting to enter the building.

I'll let the Java experts explain the best way to achieve these strategies in Java.



来源:https://stackoverflow.com/questions/30457365/example-code-detailing-difference-in-typing-between-ada-and-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!