Introduction to Static and Dynamic Typing

What is static typing? And what’s dynamic typing? I’ll answer both questions in this introductory blog.

In order to understand the difference between dynamic and static typing, we first have to look at how written programs become actual running programs.

The code you write is usually converted to some other form that a computer knows how to run. This process is called Compilation. And the period of time this happens is known as Compile time. After compilation is over, the program is launched, and the period it's running is Runtime.

Static typing

Some languages check the types and look for type errors during compile time. Those have static typing. Another way to think about it: Static typing means checking the types before running the program.

Languages: C#, C++, Java,

So if you try to create a number and try to treat it as a function in one of the languages mentioned, you'll get an error during compilation, and your program will not even try to run - it won't even get to that point because of the type error would've been found before runtime, at compile-time.

This implies that static typing has to do with the explicit declaration (or initialization) of variables before they’re employed.

/* C code */ 
int num, sum; // explicit declaration 
num = 5; // now use the variables 
sum = 10; 
sum = sum + num;

Dynamic tying

In dynamic typing, other languages check the types and look for type errors during run time. Dynamic typing means checking the types while running the program.

As you've seen before, if you employ the incorrect type, your program does run, and an error occurs only when that particular line of code is executed. So, the types are checked during the runtime.

This means that you as a programmer can write a little quicker because you do not have to specify data types every time (unless using a statically-typed language with type inference).

Examples: Perl, Ruby, Python, PHP, JavaScript

var a = 1 // int
b = 'test'// string

Hope this was useful! Let me know in the comments.

No Comments Yet