Static/Dynamic vs Strong/Weak

78,042

Solution 1

  • 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 (e.g. whether the language tries to do an implicit conversion from strings to numbers).

See the wiki-page for more detailed information.

Solution 2

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.

Solution 3

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.

Solution 4

Weak typing means that the type of an object can change depending on context. For example in a weakly typed language the string "123" may be treated as the number 123 if you add another number to it. Examples of languages with weak typing are bash, awk and PHP.

Another kind of weakly typed language is C, where the data at a memory address can be treated as a different type by casting.

In a strongly typed language the type of an object does not change - an int is always an int and trying to use it as a string will result in an error. Both Java and Python are strongly typed.

The difference between dynamic and static typing is when the type rules are enforced. In a statically typed language the type of every variable and parameter must be declared in the source and is enforced at compile time. In a dynamically typed language the types are only checked when they are used at runtime. So Java is statically typed and Python is dynamically typed.

However the boundaries can be a little blurry at times. For example although Java is statically typed, every time you use reflection or a cast (e.g. when using containers of Objects) they you are deferring the type check to runtime.

Similarly most strongly typed languages will still automatically convert between integers and floats (and in some languages abitrary precision BigInts).

Solution 5

Today researching about this subject I came across this great article http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html It cleared up a lot of things for me and I thought It may add to some of the great answers above.

Strong and Weak Typing:

Probably the most common way type systems are classified is "strong" or "weak." This is unfortunate, since these words have nearly no meaning at all. It is, to a limited extent, possible to compare two languages with very similar type systems, and designate one as having the stronger of those two systems. Beyond that, the words mean nothing at all.

Static and Dynamic Types

This is very nearly the only common classification of type systems that has real meaning. As a matter of fact, it's significance is frequently under-estimated [...] Dynamic and static type systems are two completely different things, whose goals happen to partially overlap.

A static type system is a mechanism by which a compiler examines source code and assigns labels (called "types") to pieces of the syntax, and then uses them to infer something about the program's behavior. A dynamic type system is a mechanism by which a compiler generates code to keep track of the sort of data (coincidentally, also called its "type") used by the program. The use of the same word "type" in each of these two systems is, of course, not really entirely coincidental; yet it is best understood as having a sort of weak historical significance. Great confusion results from trying to find a world view in which "type" really means the same thing in both systems. It doesn't.

Explicit/Implicit Types:

When these terms are used, they refer to the extent to which a compiler will reason about the static types of parts of a program. All programming languages have some form of reasoning about types. Some have more than others. ML and Haskell have implicit types, in that no (or very few, depending on the language and extensions in use) type declarations are needed. Java and Ada have very explicit types, and one is constantly declaring the types of things. All of the above have (relatively, compared to C and C++, for example) strong static type systems.

Share:
78,042
Daniel Revell
Author by

Daniel Revell

Updated on July 19, 2022

Comments

  • Daniel Revell
    Daniel Revell almost 2 years

    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.

  • Daniel Revell
    Daniel Revell about 14 years
    Wikipedia has all the answers. Why I've not stumbled across this already I don't know.
  • Dykam
    Dykam about 14 years
    It's a shame many are not aware that static/dynamic is something else than strong/weak... It would really save some bias and discussion.
  • Nico
    Nico almost 13 years
    (+1) for your suggestion to avoid the terms "strong" and "weak".
  • supercat
    supercat almost 12 years
    There are different degrees of "type weakness". A strongly-typed language could attempt conversions from strings to numbers. On the other hand, HyperTalk (a language I used decades ago) was so weakly typed that "12" + "34" would equal "46", but "12" + "34Q" would equal "1234Q" [fortunately, one could write "12" & "34" if one wanted concatenation]. Curiously, variables holding numbers stored them as double-precision floats, and math on such variables used the floating-point values without string munging, but there was no way to ask whether a variable was a string or number.
  • Filip Bartuzi
    Filip Bartuzi over 9 years
    I can't agree with this sentence - . "In a statically typed language the type of every variable and parameter must be declared in the source" - in SML the types of variables don't have to be declared (how ever they are checked). Lets say function f takes argument x (fun f(x)) [** so no types are declared**] and body of function is x+1. With no types declared compiler will figure out that x has to be an int. - fun f x = x + 1; val f = fn : int -> int
  • kittylyst
    kittylyst almost 9 years
    This is just flat-out wrong. Strong is not a synonym for static, nor weak a synonym for dynamic. See Norman Ramsey's answer for the actual reality.
  • Pete
    Pete almost 9 years
    @kittylyst I can't see where this answer suggests that strong is a synonym for static
  • JamesFaix
    JamesFaix over 8 years
    ++ for (roughly) single line definitions.
  • Julian A.
    Julian A. over 8 years
    great answer, and kudos for using concrete examples.
  • mehmet
    mehmet about 8 years
    Regarding C, casting is not against strong typing, but C allows for adding different types without casting too, e.g.: 5 + 'c' // OK
  • Ali Gajani
    Ali Gajani over 7 years
    This is why PHP needs to be used with great care.
  • Peter Lewerin
    Peter Lewerin over 7 years
    @mehmet: in C, character values are in the integer domain, so that particular example doesn’t violate type safety. 'c' is just syntactic sugar for 99. C doesn't have a dedicated character type.
  • mehmet
    mehmet over 7 years
    Peter Lewerin: right I should have given a better example. Unfortunately, it has been almost 20 years since I have not touched C :)
  • hagrawal
    hagrawal over 7 years
    C not a weakly typed language. It is just that Java, C# etc. are more strongly typed languages as compared to C. Read more over here - en.wikipedia.org/wiki/Strong_and_weak_typing If you check the definition of "weakly" typed language then "weakly" typed languages are those in which you can do any type of conversion, for example an int can be "implicitly" converted or casted into a string, now think yourself that whether this is possible in C or not?
  • Tarik
    Tarik about 7 years
    @Filip Bartuzi That's similar to type inference in C#. The type is implicitly provided by the coder via the use literals instead of explicitly mentioning the type.
  • Aseem Yadav
    Aseem Yadav almost 7 years
    you quoted "now at runtime if there is no type checking then I can pass an object of my own class which has same method “creditAccount(BankAccountDetails)” -- if you have already surpassed the mechanism which could have blocked you from passing an object then how type checking will stop you from calling that method in case of a statically typed language ?
  • hagrawal
    hagrawal almost 7 years
    @AseemYadav What do you mean by "* if you have already surpassed the mechanism which could have blocked you from passing an object*"?
  • Aseem Yadav
    Aseem Yadav almost 7 years
    as you mentioned it is an important security feature, and also that you could pass an object of your own class with same method , so it implies to me that it only seems to be a vulnerability when you are trying to break into someone else's code and if you're speaking in context of the code that belongs to you than its more of a performance issue than the one related to security , isn't it ?
  • hagrawal
    hagrawal almost 7 years
    There is no performance aspect of it, you have to see it from context of polymorphism then you will be able to understand security aspect of it, I have mentioned this in same paragraph.
  • jrmullen
    jrmullen over 6 years
    The language examples are really helpful. Much appreciated.
  • Bennett Keller
    Bennett Keller over 6 years
    Agreed, was just reading through Jon Skeet's book, and this is the same response noted there.
  • doubleOrt
    doubleOrt almost 6 years
    As far as I am aware, Java has these loopholes as well, but it is still considered a strongly-typed language, so I guess this lends more weight to your advice of avoiding the terms "strong" and "weak".
  • Stephen Paul
    Stephen Paul about 5 years
    In this sense would Java be ever so slightly weakly typed because you can concatenate non-strings with strings, and because of auto-unboxing / boxing?
  • mehmet
    mehmet about 5 years
    @StephenPaul you are right my answer may be understood that way, and that it is not the case. I used concatenation for the sake of simplicity, but in fact, strong-ness/weakness is about the implicit type conversion of the variable itself.
  • dhaliman
    dhaliman almost 5 years
    @Dario I do not understand what weak typing is exactly but I'm fully certain of the fact that you've misunderstood weak typing. You see, implicit conversion from strings to numbers has nothing to do with dynamic typing or weak typing but with the semantics of the PL in question.