Wednesday, November 24, 2010

Typing: Static vs Dynamic and Strong vs Weak

Programming languages can be classified based on types. It can be a weakly typed or strongly typed language. It can be statically typed or dynamically typed language.

A very simple definition of statically typed language would be languages which has typed variables. Like -
String str = "abc";
Here str is of type String. And if we assign a value to it which is not a String as in
String str = "abc";
str = 1; //would not compile
the compiler would ask us not to.
But for a dynamically typed language, variables doesn't have a fixed type. For e.g.
str = "abc"; 
str = 1;
is fine for a dynamically typed language.
Now lets look at Strongly vs Weakly typed languages. For weakly typed languages, types are not important at all. A simple point of view would be for weakly typed languages we can mix and match types. For e.g.
str = "2";
if(str==2) //would be 'true' for weakly typed languages 'false' for strong ones
There can be languages which can be Dynamically Typed but Strongly Typed like Python, Dynamically Typed and Weakly Typed like PHP, Javascript. Statically typed languages have to be Strongly typed like Java, C#.