Difference Between Statically & Dynamically Typed Languages


I’ve been doing programming for more than four years, and I’ve heard many times about statically typed and dynamically typed languages. But, honestly, I didn’t know the difference between those terms. Today, I thought I would do some research and find out the information.

So, I’ve done a little research and found out some important information regarding those topics. In this article, I would like to share what I have learned.

The difference between statically typed and dynamically typed languages is pretty simple. Before that, let’s see what type-checking is and the types of type-checking.

Type checking is the process of checking and verifying the type of a construct (constant, variable, array, list, object) and its usage context. It helps in minimizing the possibility of type errors in the program.

Type checking may occur either at compile-time (static checking) or at run-time(dynamic checking).

A programming language is statically typed if the type of a variable is known at compile time. A language is dynamically typed if the type of a variable is checked during run-time. 

So, let’s have a look at both of these terms in detail.

Statically-Typed Languages

We call a language “statically-typed” if it follows type checking during compilation. So, every detail about the variables and all the data types must be known before we do the compiling process.

In this type of language, once a variable is assigned a type, it can’t be assigned to some other variable of a different type. If we try to do so, the compiler will raise some errors, and we need to fix them. Hence, for a declared variable, the data type is fixed.

Some examples of statically-typed languages are Java, C, C++, C#, Swift, Scala, Kotlin, Fortran, Pascal, Rust, Go, COBOL, etc.

Statically Typed Languages

If you have experience in using these programming languages, you might know what I’m saying. It is common to get some type-errors while working with these languages.

In most of the statically-typed languages, the programmer must specify what the data type of each variable is, just like we do in Java, C, and C++.

But for some other languages like Hascall, Scala, and Kotlin, the type system have the in-built capability (which is called type inference) to deduce the type of a variable from the code.

Now, take a look at an example to see how a variable is declared in the C language.

int mangoes = 10;

In this example, we defined a variable name called ‘mangoes’ and specified the type as int (integer value). And, we gave it a value of 10.

This is how we declare a variable in a statically-typed language like C. In this case, the compiler can directly identify that the type of the variable is int.

One great thing about statically-typed languages is that we’ll be able to fix a lot of errors even before running the program. Once we successfully compile the program, we don’t need to worry about any kinds of syntax errors. The code will be ready to be executed.

Now the code is almost done, and the compiler knows what each data type is. Hence, it can produce an optimized machine code that uses less memory. So, the execution of the code will be faster compared to the dynamically-typed languages. These are the main advantages of statically-typed languages.

Now, let’s move ahead and take a closer look at dynamically-typed languages.

Dynamically-typed Languages

We call a language “dynamically typed” if type checking takes place while the program runs (run-time). In this type of language, there is no need to specify the data type of each variable while writing code.

It means that you can write pretty quickly since you do not have to specify types every time. Some languages do allow you to provide type information but do not require it.

Most modern programming languages are dynamically typed. Some examples of dynamically-typed languages are Python, Javascript, Ruby, Perl, PHP, R, Dart, Lua, Objective-C, etc.

Dynamically Typed Languages

A dynamically-typed language has the capability to identify the type of each variable during run-time. In these languages, variables are bound to objects at run-time using assignment statements.

We do not need to declare the data types of variables before we use them. Let’s see an example using Python.

mangoes = 10

Scripting languages are mostly dynamic, as there is no compiler to do static type-checking. However, there are chances where you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable. Fortunately, scripts tend to be tiny, and hence bugs do not have so many rooms to hide.

One advantage of dynamically-typed languages is that it is easy to write code without worrying about the type of variables. The lack of a separate compilation step implies that programmers don’t have to wait for the compiler to finish before they can test the changes in code.

The dynamic objects are having some run-time type-information(RTTI). It can be helpful to implement some pretty cool features like dynamic dispatch, down-casting, late binding, reflection, etc.

Statically typed languages have better performance at run-time and are faster at run-time. It is because there is no need for type checking at run-time, and the code has already been translated.

But dynamically-typed languages are comparatively faster during development time and are more flexible.

So, both types of languages have some unique advantages as well as disadvantages.

Conclusion

In this article, we discussed the differences between statically-typed languages and dynamically-typed languages. Here is the summary of this article in two sentences.

Statically typed languages perform type checking at compile-time, while dynamically-typed languages perform type checking at run-time. 

Statically-typed languages require you to declare the data types of your variables before you use them, while dynamically-typed languages do not.

If you would like to continue the discussion, feel free to add your thoughts in the comments section down below. Also, you can write down your doubts or queries as well.

If you want to become a better programmer, check out these 9 helpful tips to become a better programmer. I really think these tips will be useful for you.

I would appreciate it if you would be willing to share this article. It will encourage me to create more useful articles like this.

Happy coding!

Ashwin Joy

I'm the face behind Pythonista Planet. I learned my first programming language back in 2015. Ever since then, I've been learning programming and immersing myself in technology. On this site, I share everything that I've learned about computer programming.

9 thoughts on “Difference Between Statically & Dynamically Typed Languages

  1. hi! i recetnly satrted learning python and i stumbled upon this topic. i didn’t understand the thing google spit out, and latr i stumble upo your blog, and now i understand the difference between statically and dynamically typed languages! thanks!!!

    1. You can’t say that all compiled languages are all statically typed. Interpretation/compilation is a property of the language implementation, not the language. For example, Python is still the same language regardless of whether it is interpreted, compiled, both, or even not implemented at all. Python is dynamically typed. That’s a property of the language, which does not change.

  2. I think you mixed up two different concepts while trying to explain. That would be the difference between statically and dynamically typed and then loose or strong typed, regarding to being able to change a variable’s type on the run.

    1. Hi Nicky, I have removed the statements related to changing the data type, which was about strongly-typed and loosely-types languages. I hope now it is not confusing for the readers. Thanks for your suggestion.

Leave a Reply

Your email address will not be published.

Recent Posts