Can someone tell me what Strong typing and weak ty

2019-01-21 12:58发布

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

8条回答
何必那么认真
2楼-- · 2019-01-21 13:40

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

查看更多
何必那么认真
3楼-- · 2019-01-21 13:40

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.

查看更多
登录 后发表回答