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

后端 未结 8 1114
名媛妹妹
名媛妹妹 2020-12-13 03:56

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

相关标签:
8条回答
  • 2020-12-13 04:27

    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.

    0 讨论(0)
  • 2020-12-13 04:29

    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 讨论(0)
  • 2020-12-13 04:30

    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.

    0 讨论(0)
  • 2020-12-13 04:35

    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

    0 讨论(0)
  • 2020-12-13 04:39

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

    0 讨论(0)
  • 2020-12-13 04:44

    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 讨论(0)
提交回复
热议问题