Statically typed languages associate values with types at compile time (compare with DynamicallyTypedLanguages). Statically typed languages often allow for TypeInference, eliminating the need for the programmer to explicitly give all variables/bindings a data type.

Here is a list of some statically typed languages:

Are Java and C# statically typed?

Yes, because all values are typed and you can only cast from one type to a type that is higher up in the inheritance tree and casting must be explicit. --IainMcCoy

No - the mere existence of a type exception, such as a ClassCastException in Java, marks the language as dynamically typed. Runtime type errors in a statically typed language should be impossible. These languages certainly have partial static typechecking (most languages do), but a statically typed language as such needs to have complete checking at compile time. - DavidKitchin

Perhaps I misunderstand. I thought that typing was static if every object has a type and the language checks to ensure that you only perform relevant operations on objects. Must the checking be at compile-time for staticality? This suggests that the definition at the top of this page is wrong - in C#, java and even python *values* have types attached, but there is not absolute certainty as to the type of the value held within a variable. If that certainty is what static typing is about, perhaps this page should say that? --IainMcCoy

Typing is static if types are associated with bindings and are checked before the program is run. Dynamic typing associates types with the objects themselves. Checking the types of arguments to an operation is called Strong type checking, and is orthogonal to static/dynamic. For example, CommonLispLanguage is a dynamically typed language, with strong type checking: (+ 1 "a") is an error, because 1 is an integer and "a" is a string. CL is also interesting in that it allows you to have optional static type declarations, for the purpose of assisting the compiler. -- MatthewDanish

Here is a good article about the relationship between static/dynamic/strong/weak typing. -- DavidBettis

I think there is a confusion of terminology here. In Java (and also in C#) all objects have a static type, which is specified at the time of declaring the object; furthermore, this type is equal to the class the object is an instance of. Casts that obey the subtype/subclass relation have no runtime penalty or error. On the other hand, Java allows explicit coercions from super-types to sub-types, which cannot of course be statically verified in general. This feature has a considerable overhead in terms of correctness and runtime efficiency; in the absence of parametric polymorphism in Java, casting Object to other types is the sine qua non of aggregate datatypes. This particular problem is partially corrected in recent versions of Java and C# through the use of generics, so perhaps the runtime overhead of checking casts will in future be limited to unprincipled code. -- KaustuvChaudhuri

StaticallyTypedLanguages (last edited 2008-07-09 05:48:14 by localhost)