Recently (read: today) I got quite confused in separating the concepts of Dynamic vs Static and Strong vs Weak typed languages.
So I took the time to figure it out for myself.
Later I thought It would be nice to just quickly (and shortly) write down the key differences between them.
Dynamically Typed vs Statically Typed
The main difference lies in how you define variables
Statically typed
A statically typed language is a language that requires the programmer to specify the type of a variable when writing code
//JAVA String a = "It's me, Mario!; Integer b = 3; //assumed using version with autoboxing
Dynamically typed
You guessed it, you don’t have to specify the type of the variable! A very important remark is that this does not imply that the language is weakly typed, i.e. : it doesn’t mean the languages doesn’t use types!
var a = "It's me, Mario!"; var b = 3;
Strongly Typed vs Weakly Typed
The main difference here lies in how variables are used by the language
Strongly typed
This means that the language has a sense of types. Meaning that it will complain if you try to append or add an integer to a double.
In a very strongly typed language this would mean:
Strongly Statically typed
Double a = 2.0; Integer b = 3; a+b; //will give error Double d = b; //will give error
Strongly Dynamically typed
var a = 2.0; var b = 3; a+b; //will give error
Weakly typed
This bascally means that you – as a programmer – mostly don’t have to worry about types when using variables
Weakly Statically typed
Double a = 2.0; Integer b = 3; a+b; //Most languages will return 6.0 Double d = b; // the integer will be casted to double for you
Weakly Dynamically typed
var a = 2.0; var b = 3; a+b; //mostly will give 6.0
One last note:
You might have noticed that I used “very strongly typed”earlier in this post. Meaning that there are gradations between them. For example: Java is a strongly typed languages but has some weakly typed properties like appending an integer to a double. Java will automatically cast the integer to a double. Another weakly typed property of Java pops up when you are trying to concatenate strings with arbitrary objects. Here java will automatically try to convert any non-string object to a string representation.
I’ll leave exploring the (dis)advantages of different type systems to yourself!
Computer Based Math
Nice article! It seems like I’ve been using the wrong terms in the wrong contexts myself quite a few times. Still, those damned computer scientists don’t make it easy for us with all their complicated terminology and conflicting definitions!
Maybe they do it on purpose! So that only they can understand it easily
. Nevertheless, after figuring it all out, the terminology isn’t that wierd. (but still! :p )
I found your blog through StackExchange (I noticed you like Clojure!). Anyway, the topics you covered in this blog post are very tricky. There is no accepted definition for strong vs weak typing. The terms are widely used to cover many things including type safety, automatic conversions and even static vs dynamic checking. The Wikipedia article “Strong Typing” has a nice list of the different meanings. Just keep in mind that the definitions you use here for strong vs weak typing is only *one* of a set of many.
As for static vs dynamic typing, I actually think you’re still a little confused on the distinction. Static vs dynamic typing has technically nothing to do with whether the programmer has to with whether or not the programmer has to specify the type of a variable in the code (although that *is* often a good indicator). F#, Ocaml and Scala are all statically-typed languages for which support type inference, which means you don’t actually have to declare variable types. Even recent versions of C++ have some type inference support via the new “auto” keyword. On the flip side, Clojure supports optional type annotations despite being completely dynamically typed. The real distinction between static and dynamic checking is when type checking occurs. In Java, C++, F#, Ocaml and Scala the compiler type-checks the entire program at compile time. In contrast, you don’t know you have a type error in a Clojure, Python or PHP program until you actually run it and evaluate the offending expression.
You mentioned that there are varying levels of type system strength/weakness, and this is also true for static/dynamic typing. Many statically-typed languages still do dynamic checks at runtime. For example, the following Java code will pass the static type checking phase but fail dynamic checking at runtime (it causes a runtime exception):
Object[] array = (Object[]) new Integer[1];
array[0] = “Hello”;