I have read several pages, including the wiki page http://en.wikipedia.org/wiki/Strong_and_weak_typing dealing with strongly and weakly typed languages. For the most part, I think I understand the difference. However, I would like a straight to the point answer differentiating the two.
From my understanding, in weakly typed languages, data types do not have to be explicitly called. This would be a language like Matlab where you can add 4 and 2.3 without having to typecast. Strongly typed languages require the programmer to declare a data type for each variable and/or value. For instance in C, you would need to do something like 4 + (int) 2.3 or (float)4 + 2.3 (can't remember if that is valid C type-casting).
Any information expanding or correcting my understanding of these concepts would be greatly appreciated.
A weakly typed language has looser typing rules and may produce unpredictable or even erroneous results or may perform implicit type conversion at runtime.
Strongly typed is a concept used to refer to a programming language that enforces strict restrictions on intermixing of values with differing data types.
Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.
Java is a strongly typed programming language because every variable must be declared with a data type. A variable cannot start off life without knowing the range of values it can hold, and once it is declared, the data type of the variable cannot change.
The difference is not about declaring types on variables. It's a bit more subtle than that (and pace Eric Lippert, i think the term is reasonably well-defined). The distinction is that in a strongly-typed language, every expression has a type which can be determined at compile time, and only operations appropriate to that type are allowed.
In an untyped ("weakly typed" to critics, "dynamically typed" to fans) language, that is not the case. The language allows any operation to be performed on any type, with the rather substantial proviso that the operation may fail. That is, while the language may allow the operation, the runtime may not.
Note that it's possible to have a strongly-typed language without requiring type declarations everywhere. Indeed, no strongly-typed language does. Consider this bit of Java:
String s = "hellO";
int l = s.getBytes().length;
How does the compiler decide that .length
is legal there? It's legal because it's being used on a byte[]
. But there is no declaration of anything as being a byte[]
here. Rather, the compiler knows that s
is a String
, and that when you call getBytes()
on a String
, you get a byte[]
. It infers from those facts that the type of s.getBytes()
is a byte[]
, and so that it is legal to ask for its length
.
Some languages whose type systems are more sophisticated than Java's allow the compiler to infer more than this. For example, in Scala, you can say:
val s = "hello"
val l = s.getBytes().length
And the compiler will infer the types of s
and l
, as well as of the intermediate expressions.
Languages which have strong typing but artificial limits on type inference which require redundant type declarations (like Java) are described as having manifest typing, because the types must be made manifest, which is a fancy way of saying explicitly brought into existence, which is a fancy way of saying written down.
Check Eric Lippert's blog out. There's an entry about just what you're looking for here.
From the looks of his blog, those terms are subjective, so "speak more precisely about type system features."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With