40

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

2

8 Answers 8

53

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 :)

5
  • 16
    "and I can put it in a string" nice. +1 for the weak side of the force
    – annakata
    Nov 27, 2008 at 11:23
  • 2
    "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". Aug 6, 2009 at 10:56
  • 2
    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. Aug 6, 2009 at 10:59
  • @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. Apr 17, 2010 at 22:58
  • 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}.". Oct 15, 2011 at 23:12
14

"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.

5

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

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.

2

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.

1
  • 1
    This one is spot on.
    – isekaijin
    Nov 16, 2019 at 13:15
1

Strong/weak typing in a language is related to how easily you can do type conversions:

For example in Python:

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

Where as in C language:

int a = 5;
a = 5 + 'c';
/* is fine, because C treats 'c' as an integer in this case */

Thus Python is more strongly typed than C (from this perspective).

0

May be this can help you to understand Strong and Weak Typing.......

Strong typing: It checks the type of variables as soon as possible, usually at compile time. It prevents mixing operations between mismatched types.

A strong-typed programming language is one in which:

  • All variables (or data types) are known at compile time
  • There is strict enforcement of typing rules (a String can't be used where an Integer would be expected)
  • All exceptions to typing rules results in a compile time error

Weak Typing: While weak typing is delaying checking the types of the system as late as possible, usually to run-time. In this you can mix types without an explicit conversion.

A "weak-typed" programming language is simply one which is not strong-typed.

which is preferred depends on what you want. for scripts and good stuff you will usually want weak typing, because you want to write as much less code as possible. in big programs, strong typing can reduce errors at compile time.

-2

Weak typing means that you don't specify what type a variable is, and strong typing means you give a strict type to each variable.

Each has its advantages, with weak typing (or dynamic typing, as it is often called), being more flexible and requiring less code from the programmer. Strong typing, on the other hand, requires more work from the developer, but in return it can alert you of many mistakes when compiling your code, before you run it. Dynamic typing may delay the discovery of these simple problems until the code is executed.

Depending on the task at hand, weak typing may be better than strong typing, or vice versa, but it is mostly a matter of taste. Weak typing is commonly used in scripting languages, while strong typing is used in most compiled languages.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.