What is the different between static and dynamic programming languages? I know that it is all about type systems but I’m looking for more clear clarifications.
Statically Typed Languages This means that before source code is compiled, the type associated with each and every single variable must be known. Some common examples of programming languages that belong to this category are Java, Haskell, C, C++, C#, Scala, Kotlin, Fortran, Go, Pascal, and Swift.
In computer terminology, dynamic usually means capable of action and/or change, while static means fixed.
Python is a dynamically typed language which means checking of the variable is done at the runtime. Whereas in the Statically typed language the checking of the variables or any other is done at the compile time.
Some examples of statically-typed languages are Java, C, C++, C#, Swift, Scala, Kotlin, Fortran, Pascal, Rust, Go, COBOL, etc.
Static typing means that types are known and checked for correctness before running your program. This is often done by the language's compiler. For example, the following Java method would cause a compile-error, before you run your program:
public void foo() { int x = 5; boolean b = x; }
Dynamic typing means that types are only known as your program is running. For example, the following Python (3, if it matters) script can be run without problems:
def erroneous(): s = 'cat' - 1 print('hi!')
It will indeed output hi!
. But if we call erroneous
:
def erroneous(): s = 'cat' - 1 erroneous() print('hi!')
A TypeError
will be raised at run-time when erroneous
is called.
Difference between static and dynamic is that before running the program if the data type of each variable is checked and verified then it's static type programming language (e.g:- in case of C++ it's done by the compiler). In Dynamic programming language during runtime, if there is an invalid assignment of a variable that violates its data type then an error is given for that.
Summary- Static type language check any violation before running the program whereas in the dynamic type language the error is given when the program is running and goes to the part were violation has been done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With