First of all, what are all these words - Statically - Dynamically - Strongly - Weakly Typed Languages?
This is how you can classify programming 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 :
Type Checking
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?Â
Type declaration
Static: All variable types have to be explicitly stated as this information is required at compile time.
For example in Java Â
Static: All variable types have to be explicitly stated as this information is required at compile time.
For example in Java Â
float f = 0.5;
Dynamic: Explicit declaration is not required as type is assigned to a variable at runtime.
For example in Python
For example in Python
f = 0.5
Performance
Static: Do more processing at compile time but give better run-time performance.
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.
Static: Is less prone to runtime errors but provide less flexibility to programmer.
Dynamic: Provides more flexibility but more prone to runtime errors.
Remember It
A quick hack to remember what are statically typed and dynamically typed language, is to call them by their full names
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.
The language cloud. Courtesy: Mayank Bhatnagar.
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.Â
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.
For example in JavascriptÂ
$data = “string1” //Type assigned as str at runtime
$data = 5 //Type assigned as int at runtime
$data = $data+“string2” //str and int get concatenated
Conclusion
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.
(Fun?) Disclaimer
If you go into the nitty gritty and try to find THE right answer, you will come back discombobulated (exactly!).
Turns out, there is no official demarcation of these terms agreed throughout the industry. It’s a spectrum.
But the terms still get thrown around a lot. So, I went digging. Java Language Specification calls Java a strongly typed language.
But The C Programming Language First Edition uses no such term.
References
- medium.com/p/magic-lies-here-statically-typed-vs-dynamically-typed-languages
- docs.oracle.com/javase/specs/jls/se8/jls8.pdf
- archive.org/details/TheCProgrammingLanguageFirstEdition/page/n7
- stackoverflow.com/a/430414/4693983
- stackoverflow.com/a/1520342/4693983
- blogs.msdn.microsoft.com/alfredth/2012/02/17/how-to-start-a-computer-science-flame-war/
- merriam-webster.com/dictionary/discombobulate
- medium.com/@haydnjmorris/page-2-dynamically-typed-vs-statically-typed-languages-e507ac463496
Find me on Twitter, Facebook, Linkedin, Quora, Github, Medium, Gmail @jarpit96