Weak And Strong Typing

What is the difference between a weak and strong type?

In a weakly typed language, the type of a value depends on how it is used. For example if I can pass a string to the addition operator and it will AutoMagically be interpreted as a number or cause an error if the contents of the string cannot be translated into a number. Similarly, I can concatenate strings and numbers or use strings as booleans, etc.

In a strongly typed language, a value has a type and that type cannot change. What you can do to a value depends on the type of the value.

The advantage of a strongly typed language is that you are forced to make the behaviour of your program explicit. If you want to add a number and a string your code must translate the string into a number to be used as an operand of the addition operator. This makes code easier to understand because there is no (or less) hidden behaviour. However, it also makes your code more verbose.

The advantage of a weakly typed language is that you need to write less code. However, that code is harder to understand because a lot of its behaviour is hidden in implicit type conversions.

Strong vs. weak typing is comparable to static vs. dynamic typing. In a statically typed language, type checking is performed at compile time; in a dyamically typed language type checking is performed at run time. In practice, weakly typed languages are usually dynamically typed.

All languages can be placed somewhere in the 2D "typing" space whose axes are strong <-> weak and static <-> dynamic. E.g. types in SmalltalkLanguage are very strong, completely dynamic; types in PHP are quite weak, completely dynamic; types in LISP are completely strong, completely dynamic; types in C are quite weak, completely static; and so on...
There are two different issues that should be separated out:
See TypingQuadrant for a diagrammatic representation complete with examples
CategoryLanguageTyping

EditText of this page (last edited September 12, 2012) or FindPage with title or text search