воскресенье, 14 февраля 2010 г.

"The intention of saying Java is strongly typed is to give the impression of robustness"

Смотрю статью на википедии о языке Ява: Typing discipline: Static, strong, safe, ...

Java SE 6 используется полным ходом, уже скоро на подходе SE 7, а статьи 11-летней давности (by Harold Thimbleby), похоже, актуальны и по сей день.

Java is strongly typed, but the type correctness of a Java program is not known at compile time. In other words, Java is not statically typed (like ML). The intention of saying Java is strongly typed is to give the impression of robustness. Type robustness would be achieved by strong static typing, where the compiler detects type errors before a program is run. Like Java, both BASIC and LISP are strongly typed, but neither are statically typed; their type systems do not seem to be what the Java hype seems to imply! Our discussion, above, of the duck and lame duck's equals method illustrated some problems of Java not being statically typed.

The following example demonstrates Java's lack of strong static typing; it involves creating an Object and casting it to a Character. This is statically correct (i.e., it compiles without error), but at run time it throws an error (a ClassCastException), because Objects are not Characters. (Sophisticated compilers might detect the problem (though whether they -- and they alone -- should is an interesting question of compatibility), but they can easily be defeated by passing an Object as a parameter to a method expecting a Character: the result will be the same.)
Character c = (Character) new Object();
// always causes runtime java.lang.ClassCastException
Apparently the assignment is not questionable, for
"Java prevents incompatible assignments by forbidding anything questionable." p121

Комментариев нет: