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”
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
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
- 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
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”
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
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
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
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
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
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
Static Typing and Dynamic 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
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
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.
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.
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
Weak Typing and Strong 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++
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.
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.
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
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 :)