DEV Community

Cover image for You Need To Know Compiled/Interpreted, Static/Dynamic, and Strong/Weak Typing
Rahul Banerjee
Rahul Banerjee

Posted on • Originally published at realpythonproject.com

You Need To Know Compiled/Interpreted, Static/Dynamic, and Strong/Weak Typing

In this article, I’ll try to demystify the terms static/dynamic typing, strong/weak typing, and define some terms like run-time, compilation, etc

If you have taken a few programming courses, you might have come across the following statements and somewhat understood

  • “Python is strongly, dynamically typed”
  • “JavaScript is weakly, dynamically typed”

After reading this article, you will be able to clearly understand the above statements. We will be looking at code snippets in Python, JavaScript and C++. If you do not know the syntax or any of the languages, it’s ok, you will still be able to understand the underlying concepts. 

Table of Contents

  • Some Definitions and Points to Remember
  • Compiled vs Interpreted Languages
  • Static Typing vs Dynamic Typing
  • Weak Typing vs Strong Typing
  • Classifying Programming Languages
  • Conclusion

Some Definitions and Points to Remember

  • The word “typing” in static typing/dynamic typing refers to the DATATYPE. It doesn’t refer to the act of pressing keys. I was confused about this and I am sure there might be others in the same boat
  • Programming languages are classified as Static/Dynamic types and Strong/Weak type based on two different properties, i.e Static/Dynamic typing is not related to Strong/Weak typing

Definitions

  • Source Code- This is the code written in Python, JavaScript, C++, etc. Also known as human-readable code
  • Machine Code- This is the code that is written in Machine Language (usually binary) and can be understood by a Machine. 
  • Compiler- A computer program that can convert the source code into Machine Code
  • Compilation- Transforming code from Python, JavaScript, C++ to low-level machine code. Basically converting human-readable code to machine code

Assuming compilation is successful, i.e the source code is syntax-error free, we move on to Runtime.

  • Runtime- After compilation, the program runs, and the code is executed. This time period is known as runtime.
  • Execution- Actually running/executing the code. This happens after the source code is converted to machine code and during Runtime
  • Syntax Error- Error in the syntax (grammar) of the language. Eg: missing a semicolon in C++, an improper indentation in Python, etc
  • Run-time Errors- A runtime error is an error that occurs during program execution eg: division by 0, trying to access an undefined variable, using an operator on incompatible datatypes
  • TypeCasting- Converting a value from one data type to another, eg: a char can be converted to an int based on its ASCII value. Similarly a number 10 can be converted to a string “10”. Type casting can either be implicit (it is done by the language automatically) or explicit (you manually change the type)

Compiled vs Interpreted Languages

A language is classified as compiled/interpreted based on how the source code is converted to machine code.

I’ll borrow this analogy from FreeCodeCamp.

Basically consider you have a recipe written in German and need help from your friend to translate the recipe to English and follow it. There are a couple of ways you could go about this

  • You could translate each step one by one, i.e first your friend translates Step 1 and you follow it. Then you move on to the second step, your friend translates it and you follow it. And so on…..
  • You translate the entire recipe at once and then follow it, i.e your friend translates the entire recipe to English. Now, you follow it step by step.

The first approach is similar to “Interpreted” while the second approach is similar to “Compiled”

Interpreted Languages

The program’s source code is converted to machine code and executed line by line. If you define a function but never invoke it, the function is not executed and as a result, any run-time error in the function will not result in an error.

Let’s consider an example in Python

Code Snippet

Although our function has a division by 0 (Runtime Error), the program runs/executes successfully. Since we never call the function func, the function is never executed and hence the runtime error is never raised

Let’s consider a similar example in JavaScript

Code Snippet

Like Python, JavaScript doesn’t raise any errors either. The program is executed successfully since we never invoke the function.

Note: If you did invoke the function in any of the above code-snippets, it would cause an error

Let’s consider another example in Python

Code Snippet

This time, we have a division by 0 outside a function. This causes Python to raise an error. However, noticed that the print statements are still executed and their output is shown in the terminal. This is because each line is executed one by one. Therefore, when we are at the third print statement, Python hasn’t even executed the division by 0 and as a result, no error is raised till that point

Compiled Languages

The entire source code is converted to machine code. After the conversion, the program is executed. Therefore any runtime error in a function is raised, irrespective of whether the function is invoked or not. 

Let’s consider a C++ code snippet

Code Snippet

In the function func, we assign a value 10 to variable var without defining the variable (Runtime Error). Although we don’t invoke the function, the error is still raised. “Hello World” is not displayed in the terminal.

Let’s consider another snippet

Code Snippet

Unlike Python or JavaScript, the “cout” statement is not executed. First, the entire source code is converted to machine code and the run-time error is raised. As a result, “Hello World” is not displayed in the terminal. The error is raised since we have not defined the variable var

Static Typing and Dynamic Typing

Static Typing

In statically typed languages, a variable has a FIXED data type during its lifecycle. 

Type Checking is done during compile-time

Consider the C++ code snippet below

Code Snippet

Initially, we declare variable var1 as an int and assign it an integer value. But later, we re-assign it a string. C++ is a statically typed language and doesn’t support this behavior. 

Eg: C, C++, Java, Rust, Go, Scala

Dynamic Typing

In dynamically typed languages, the data type of a variable can be changed during it’s life cycle

Type Checking is done during run-time

Let’s try to do the same thing we did above but in a dynamically typed language like Python.

Code Snippet

Initially variable var1 is an integer variable. However, later we assign a string value and the data type of the variable changes. A dynamically typed language like Python allows us to change the data type of a variable.

Let’s do something similar in JavaScript, another dynamically typed language.

Code Snippet

Note: if you use the const keyword, you can not change the data type of the the variable. Constant values can not be re-assigned, therefore in a way their data type is static. However, Java Script is a dynamically typed language since the data type of a VARIABLE can change during it’s lifecycle

Eg: Perl, Ruby, Python, PHP, JavaScript

Weak Typing and Strong Typing

Weak Typing

Weakly typed languages support operations between different data types. These languages support implicit type casting and one of the data types is converted to the other.

Let’s consider a code snippet in C++

Code Snippet

The ascii value of a is 97. When we try to do 10+a, C++ implicitly typecasts a to it’s ascii value(97). Therefore the output is 107.

Let’s do something similar in JavaScript.

Code Snippet

JavaScript convert the number 10 to a string “10” and concatenaded it to a

Eg: C/C++, JavaScript, PHP, Perl

Strong Typing

Strongly typed languages DO NOT support operations between different datatypes. They might/might not support typecasting. However, they do not implicitly typecast data.

Let’s consider a code snippet in Python. We’ll try to do the same thing we did above.

Code Snippet

When we try to add an integer and a string, we get a Type Error. This makes Python Strongly type. However, notice that it is possible to explicitly typecast the integer to a string and perform the operation.

Eg: Python, C#, Java, Scala, Ruby

Classifying Programming Languages

https://medium.com/android-news/magic-lies-here-statically-typed-vs-dynamically-typed-languages-d151c7f95e2b

Conclusion

I hope I was able to explain the terms and how a language is determined to be compiled/interpreted, static/dynamic, weak/strong. Knowing these properties about a language will let you know what to expect when starting out with that language. Please let me know if you find any mistakes or know of any good analogies to explain the difference :)

Connect with me on LinkedIn, Twitter

Top comments (0)