In this article, we are going to discuss the static and dynamic typing. I will make references of the programming language like Perl, PHP and Python, however, you do not have to know any particular programming language to understand these concepts. Who don’t know the particular programming language, you should think of the code as pseudocode.
"typing" in terms of static and dynamic typing refers to "type" as in data type — not the process of pressing keys on a keyboard. And the languages that have static typing or dynamic typing are said to be "static typed" or "dynamic typed".
Static typed programming languages are those in which variables need not be defined before they’re used i.e. explicit declaration (or initialization) of variables required before they’re used. Java, C, and C++ are the examples of a static typed language.
Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:
The above code is an example of how variable declaration in static typed languages generally appears.
Note that, int has been used to initialize the num1, num2 and sum to zero.
Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that we do not require the explicit declaration of the variables before using them. Python is an example of a dynamic typed programming language, and so is PHP.
In the above code, we have directly assigned the variable num the value 10 before initializing it. This is characteristic to dynamic typed programming languages.
A lot of people define static typing and dynamic typing with respect to the point at which the variable types are checked. Using this analogy, static typed languages are those in which type checking is done at compile-time, whereas dynamic typed languages are those in which type checking is done at run-time.
This analogy leads to the analogy we used above to define static and dynamic typing. I believe it is simpler to understand static and dynamic typing in terms of the need for the explicit declaration of variables, rather than as compile-time and run-time type checking.
Static Typing and Dynamic Typing versus Strong Typing and Weak Typing
Static and dynamic typing, and strong and weak typing, are two totally different concepts, which, unfortunately, are very often confused. It is erroneous to say that a language that is static or dynamic typed cannot be strong or weak typed. Static and dynamic typing, and strong and weak typing, are different forms of classification of programming languages, and one of each class necessarily characterizes a given language. It is thus imperative to discuss strong and weak typing vis-a-vis static and dynamic typing.
Programming languages that exhibit "strong typing" are "strong typed," and those that exhibit "weak typing" are "weak typed".
Programming languages in which variables have specific data types are strong typed i.e. variables are necessarily bound to a particular data type. Python is strong typed, and so is Java. Consider the following example:
In the above example, foo is of str type. In the second line, we’re attempting to add 2 (an int) to a variable of str type. As we can see, a TypeError is returned, indicating that a str object cannot be concatenated with an int object. This is what characterizes strong typed languages: variables are bound to a particular data type.
As opposed to strong typed languages, weak typed languages are those in which variables are not of a specific data type i.e. variables are not "bound" to a specific data type. PHP and C are examples of weak typed languages. Consider the following:
In this example, foo is initially a string type. In the second line, we add this string variable to 2, an integer. This is permitted in PHP, and is characteristic of all weak typed languages.
Now that we know about the two concepts, we can augment both of them to characterize any given language. Thus, Python is dynamic typed and strong typed; Java is static typed and strong typed; PHP is dynamic typed and weak typed; C is static typed and weak typed (owing to its casting ability).
Why Dynamic Typing?
In a dynamic typed language, you don’t have to initialize variables, as programmers can use a variable at will when required (without having to initialize it) which is a big bonus for many developers. Dynamic typing is characteristic of many of the scripting languages: Perl, PHP, Python, etc. Dynamic typing, in fact, does save you from writing a few "extra" lines of code, which, in turn, means less time spent writing code.
Problem with the Dynamic typing?
The very characteristic of dynamic typed languages that appeal to many developers is also a pitfall and a major one at that.
As we can observe in the above code, varaible is a spelling mistake and it leads to a problem here, since Python is dynamically typed, it will not return an error, but instead will create a new variable called variable and now we have two variables: variable and varaible. This obviously is a serious problem; some would suggest that forced variable declaration is an important requirement in any programming language.
Static Typed Behavior in Dynamic Typed Languages
Perl is also a dynamic typed programming language. However, it provides a means to "simulate" static typing by means of a pragma called strict.
The above code will run without any problem and will print 10 to the console. Note that here, we have not initialized the variable sum; this exemplifies the dynamic typing characteristic of Perl.
To enforce variable declaration, we make use of the strict pragma as follows:
The above code will return the following error when you try to run it:
Global symbol "$num" requires explicit package name at perl.pl line 2.
Execution of perl.pl aborted due to compilation errors. To rectify the above error, we are forced to declare the variable num as follows:
The above codes are specific to Perl; not all programming languages have a way to enforce variable declaration: Python, for example, doesn’t have a way to enforce variable declaration. However, there is a tool, called "pychecker", that can be used to "detect" stray variables; this is, of course, far from a desirable solution.
Static Typed or Dynamic Typed?
This is highly debating topic as there are advocates of both forms of typing. Asserting that one is better than the other would only lead to a debate of no consequence.
There are those who advocate dynamic typing for the simplicity and saving in terms of time that it offers; they believe that type checking need not be an integral part of the programming language design per se, but instead, that third-party solutions (like pychecker) could be used to server that purpose.
On the other hand, there are advocates of static typing, who believe that static typing (leading to forced variable declaration) is an important requirement of programming language design.