Can someone tell me what Strong typing and weak typing means and which one is better?

45,639

Solution 1

That'll be the theory answers taken care of, but the practice side seems to have been neglected...

Strong-typing means that you can't use one type of variable where another is expected (or have restrictions to doing so). Weak-typing means you can mix different types. In PHP for example, you can mix numbers and strings and PHP won't complain because it is a weakly-typed language.

$message = "You are visitor number ".$count;

If it was strongly typed, you'd have to convert $count from an integer to a string, usually with either with casting:

$message = "you are visitor number ".(string)$count;

...or a function:

$message = "you are visitor number ".strval($count);

As for which is better, that's subjective. Advocates of strong-typing will tell you that it will help you to avoid some bugs and/or errors and help communicate the purpose of a variable etc. They'll also tell you that advocates of weak-typing will call strong-typing "unnecessary language fluff that is rendered pointless by common sense", or something similar. As a card-carrying member of the weak-typing group, I'd have to say that they've got my number... but I have theirs too, and I can put it in a string :)

Solution 2

"Strong typing" and its opposite "weak typing" are rather weak in meaning, partly since the notion of what is considered to be "strong" can vary depending on whom you ask. E.g. C has been been called both "strongly typed" and "weakly typed" by different authors, it really depends on what you compare it to.

Generally a type system should be considered stronger if it can express the same constraints as another and more. Quite often two type systems are not be comparable, though -- one might have features the other lacks and vice versa. Any discussion of relative strengths is then up to personal taste.

Having a stronger type system means that either the compiler or the runtime will report more errors, which is usually a good thing, although it might come at the cost of having to provide more type information manually, which might be considered effort not worthwhile. I would claim "strong typing" is generally better, but you have to look at the cost.

It's also important to realize that "strongly typed" is often incorrectly used instead of "statically typed" or even "manifest typed". "Statically typed" means that there are type checks at compile-time, "manifest typed" means that the types are declared explicitly. Manifest-typing is probably the best known way of making a type system stronger (think Java), but you can add strength by other means such as type-inference.

Solution 3

I would like to reiterate that weak typing is not the same as dynamic typing.

This is a rather well written article on the subject and I would definitely recommend giving it a read if you are unsure about the differences between strong, weak, static and dynamic type systems. It details the differences much better than can be expected in a short answer, and has some very enlightening examples.

http://en.wikipedia.org/wiki/Type_system

Solution 4

Strong typing is the most common type model in modern programming languages. Those languages have one simple feature - knowing about type values in run time. We can say that strong typed languages prevent mixing operations between two or more different kind of types. Here is an example in Java:

String foo = "Hello, world!";
Object obj = foo;

String bar = (String) obj;
Date baz = (Date) obj; // This line will throw an error

The previous example will work perfectly well until program hit the last line of code where the ClassCastException is going to be thrown because Java is strong typed programming language.

When we talk about weak typed languages, Perl is one of them. The following example shows how Perl doesn't have any problems with mixing two different types.

$a = 10;
$b = "a";
$c = $a . $b;
print $c; # returns 10a

I hope you find this useful,

Thanks.

Solution 5

This article is a great read: http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html Cleared up a lot of things for me when researching trying to answer a similar question, hope others find it useful too.

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:
45,639
zamfir
Author by

zamfir

this area is intended to be blank.

Updated on June 02, 2020

Comments

  • zamfir
    zamfir almost 4 years

    Can someone tell me what Strong typing and weak typing means and which one is better?

  • annakata
    annakata over 15 years
    "and I can put it in a string" nice. +1 for the weak side of the force
  • Paul Biggar
    Paul Biggar almost 15 years
    "Strong-typing means that you can't use one type of variable where another is expected". Strong/weak aren't about variables, they are about values. I think you should say: "Strong-typing means that you can't use one type of value where another is expected".
  • Paul Biggar
    Paul Biggar almost 15 years
    You are confusing static typing and strong typing. "Communicating the purpose of a variable" is static typing. "Unnecessary language fluff" is static typing. Avoiding bugs is largely static typing, but strong typing also helps here, so half-marks.
  • Lawrence Hutton
    Lawrence Hutton about 14 years
    @Paul: I would argue that "communicating the purpose of a variable" is good naming. A variable named interest_rate (or interestRate or $interest_rate or however you want to capitalize/space it) communicates its purpose quite clearly, regardless of whether it is strongly- or weakly-typed, static or dynamic.
  • Peter Becker
    Peter Becker over 12 years
    Java let's you concatenate a number (or any object) to a string, despite it being typed rather strongly (see my answer on the lack of meaning of that). I agree with Paul that you seem to talk more about static typing, but even then you can define string concatenation implicitly (as Java does). It's still a bad idea ;-) String interpolation is so much nicer and controllable: "You are visitor number ${count}.".
  • pyon
    pyon over 4 years
    This one is spot on.