Warm tip: This article is reproduced from serverfault.com, please click

Difference between static and dynamic programming languages

发布于 2013-12-13 09:50:08

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.

Questioner
Balaji Reddy
Viewed
0
sleeparrow 2016-02-29 02:15:23

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.