How To Understand The Difference Between Statically - Dynamically - Strongly - Weakly Typed Language
First of all, what are all these words - Statically - Dynamically - Strongly - Weakly Typed Languages?
This is how you can classify programming languages:
- Statically typed vs Dynamically typed programming languages
- Strongly typed vs Weakly typed programming languages.
We will understand what these terms mean so you don't have to look them up in your next flame war
So let’s dive right in :
It’s the process of verifying and enforcing the constraints of types. Usually performed by the compiler or interpreter at compile or run time. For instance you can’t divide a string by a floating point number (if you can, please don't).
In simpler terms, type checking is just looking at variables and their types and then saying does this expression make sense.
So, now we know what type checking is, understanding these terms is really simple.
In statically typed languages type checking happens at compile time. Whereas, in dynamically typed languages, type checking happens at run-time.
What Does That Mean For You?
All variable types have to be explicitly stated as this information is required at compile time.
For example in Java
float f = 0.5;
Explicit declaration is not required as type is assigned to a variable at runtime.
For example in Python
f = 0.5
Static: Do more processing at compile time but give better run-time performance.
Dynamic: More efficient compiler/interpreters, but type-checking at run-time affects performance.
Flexibility and Errors
Static: Is less prone to runtime errors but provide less flexibility to programmer.
Dynamic: Provides more flexibility but more prone to runtime errors.
A quick hack to remember what are statically typed and dynamically typed language, is to call them by their full names
==> Statically type-checked languages.
==> Dynamically type-checked languages.
But What are Strongly Typed and Weakly Typed Languages?
It’s a spectrum* (disclaimer at the bottom). So, we will just go ahead and learn the terms the way they are often used.
In Strongly typed languages once a type is assigned to a variable say at run time or compile time, it retains that type and can’t be intermingled in expressions with other types easily.
For example in Python
data = “string1” //Type assigned as str at runtime
data = 5 //Type assigned as int at runtime
data = data + “string2” //Type-error str and int can’t be concatenated
Whereas, in Weakly typed languages, once a type is assigned to a variable say at run-time or compile-time, it can be intermingled in expressions with other types easily.
$data = “string1” //Type assigned as str at runtime
$data = 5 //Type assigned as int at runtime
$data = $data+“string2” //str and int get concatenated
Type checking ensures that correct variable types are part of an expression. In Statically type-checked languages, type checking happens at compile time, whereas in dynamically type-checked languages, type checking happens at run-time.
Strongly typed languages have a stronger type-check and enforce the type assigned (at compile or run time), whereas, weakly typed languages have a weak type-check and allows expressions between various different types.
If you go into the nitty gritty and try to find THE
right answer, you will come back discombobulated
Find me on Twitter, Facebook, Linkedin, Quora, Github, Medium, Gmail @jarpit96
Subscribe to get your daily round-up of top tech stories!