Difference between static and dynamic programming languages

Balaji Reddy picture Balaji Reddy · Dec 13, 2013 · Viewed 53.8k times · Source

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.

Answer

sleeparrow picture sleeparrow · Dec 11, 2015

Static Typing

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

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.