Booklet
Static VS Dynamic Typing
Static Vs Dynamic Typing
To start off...
Let's not confuse Static / Dynamic Typing with Strong / Weak Typing.
They're similar in a way but fundamentally different.
Static / Dynamic Typing is about when type information is acquired (either at compile time or at runtime).
Strong / Weak Typing is about how strictly types are distinguished. (For example: whether the language tries to do an implicit conversion from strings to numbers.)
Now that we cleared that up, let's go deeper into Static vs Dynamic Typing.
Static Typing
Essentially, Static Typing means that variable types are checked at "compile time", or before the code is actually executed.
Let's look at this Java example:
public class FooClass {
static void foo (String name) {
System.out.println("My name is" + name);
}
}
Here, the foo function takes in a String parameter. Attempting to pass anything other than a string into the function after explicitly stating it accepts only a string, will cause it to throw a compiler error.
Examples of statically-typed languages:
1. Java
2. C
3. C++
4. FORTRAN
5. Pascal
6. Scala
Dynamic Typing
On the other hand, dynamic typing means that the variables' types are checked on the fly, as the code is executed..
Let's look at this JavaScript example:
function foo (num) {
if (typeof num === 'Number') {
return true;
} else {
return false;
}
}
Depending on what value is passed into the function foo, the variable num could technically be any type, and it'll only be known which one when the code is actually executed.
Examples of
dynamically-typed
languages:
1. JavaScript
2. Objective-C
3. PHP
4. Python
5. Ruby
6. Lisp
7. Tcl
Next post...
We'll dive deeper into the difference between Strong and Weak Typed languages.
So be on the lookout!
Join our tech and design community! @hackuniversity