Java and Static Typing

, May 18, 2004

Charles Miller thinks Java’s implementation of static typing is stupid. I’m not sure I agree; while I like languages with dynamic typing/type inference, I’m not at all sure loosening up the restrictions in a statically typed language will lead to a clean design.

On May 18, 2004 10:17 AM, Charles Miller said:

Dynamic typing != type inference. Dynamic typing means that the compiler performs no type checking whatsoever: everything is deferred to runtime. Type inference means that the compiler infers the type of all objects from context, and enforces type-safety based on that inference.

Read the “static typing in Perl” slides I linked to from the bottom of the article, they’re a good example of the difference, showing how type inference can be applied to a dynamic language as well.

The beauty of type inference is that there’s no loosening up. Everything still ends up being associated with a type at compile-time, and the compiler still enforces all the restrictions of static typing. The only difference is that the types are implied rather than express.

On May 18, 2004 10:44 AM, Stefan Tilkov said:

I could worm my way out of this by saying that I didn’t claim that the two are equivalent, just that I like both; but you are right that I didn’t really think about the distinction when I wrote the statement above.

Maybe ‘loosening up’ is the wrong term. Java, as it is, follows C’s (or rather C++’s) tradition, which mentions and requires types everywhere and very explicitly. To change Java to infer types from variables’ usage would break that tradition and create sort of a half-step towards a more dynamic (although not dynamically typed) language.

On May 18, 2004 12:15 PM, Adrian Howard said:

But is it a useful tradition?

Do the explicit declarations actually help code comprehension - or are they just noise that gets in the way?

On May 18, 2004 4:28 PM, Stefan Tilkov said:

To a large degree, that is probably a matter of taste; to really answer these questions, one would have to compare a version with and without the extra type info.