Static/Dynamic vs Strong/Weak

I see these terms bandied around all over the place in programming and I have a vague notion of what they mean. A search shows me that such things have been asked all over stack overflow in fact. As far as I'm aware Static/Dynamic typing in languages is subtly different to Strong/Weak typing but what that difference is eludes me. Different sources seem to use different meanings or even use the terms interchangeably. I can't find somewhere that talks about both and actually spells out the difference. What would be nice is if someone could please spell this out clearly here for me and the rest of the world.


  • Static/Dynamic Typing is about when type information is acquired (Either at compile time or at runtime)

  • Strong/Weak Typing is about how strictly types are distinguished (eg whether the language tries to do an implicit conversion from strings to numbers).

  • See the wiki-page for more detailed information.


    You have discovered a soft spot in the terminology that amateurs use to talk about programming languages. Don't use the terms "strong" and "weak" typing , because they don't have a universally agreed on technical meaning. By contrast, static typing means that programs are checked before being executed , and a program might be rejected before it starts. Dynamic typing means that the types of values are checked during execution , and a poorly typed operation might cause the program to halt or otherwise signal an error at run time . A primary reason for static typing is to rule out programs that might have such "dynamic type errors".

    Strong typing generally means that there are no loopholes in the type system, whereas weak typing means the type system can be subverted (invalidating any guarantees). The terms are often used incorrectly to mean static and dynamic typing. To see the difference, think of C: the language is type-checked at compile time (static typing), but there are plenty of loopholes; you can pretty much cast a value of any type to another type of the same size---in particular, you can cast pointer types freely. Pascal was a language that was intended to be strongly typed but famously had an unforeseen loophole: a variant record with no tag.

    Implementations of strongly typed languages often acquire loopholes over time, usually so that part of the run-time system can be implemented in the high-level language. For example, Objective Caml has a function called Obj.magic which has the run-time effect of simply returning its argument, but at compile time it converts a value of any type to one of any other type. My favorite example is Modula-3, whose designers called their type-casting construct LOOPHOLE .

    Having said that, you can't count on any two people using the words "strong" and "weak" in exactly the same way. So avoid them.


    Simply put it this way: in a statically typed language the type is static, meaning once you set a variable to a type, you CANNOT change it. That is because typing is associated with the variable rather than the value it refers to.

    For example in Java:

    String str = "Hello";  //statically typed as string
    str = 5;               //would throw an error since java is statically typed
    

    Whereas in a dynamically typed language the type is dynamic, meaning after you set a variable to a type, you CAN change it. That is because typing is associated with the value rather than the variable.

    For example in Python:

    str = "Hello" # it is a string
    str = 5       # now it is an integer; perfectly OK
    

    On the other hand, the strong/weak typing in a language is related to implicit type conversions (partly taken from @Dario's answer):

    For example in Python:

    str = 5 + "hello" 
    # would throw an error since it does not want to cast one type to the other implicitly. 
    

    whereas in PHP:

    $str = 5 + "hello"; // equals 5 because "hello" is implicitly casted to 0 
    // PHP is weakly typed, thus is a very forgiving language.
    

    Static typing allows for checking type correctness at compile time. Statically typed languages are usually compiled, and dynamically typed languages are interpreted. Therefore, dynamicly typed languages can check typing at run time.

    链接地址: http://www.djcxy.com/p/72308.html

    上一篇: 是否有编译*编程语言与动态,甚至可能弱输入?

    下一篇: 静态/动态与强/弱