静态/动态类型语言 & 强/弱类型语言

来自

静态/动态类型语言

  • Static/Dynamic typing is about when type information is aquired (Either at compiletime or at runtime)
  • 静态类型语言比较不容易出差错,很多问题能提前检查出来。而动态语言比较灵活,用起来比较浪

In a statically typed language the type is static, meaning once you set a variable to a type, you CANNOT change it. That is because typing is associated with the value rather than the variable. (静态类型语言中类型与变量是一个整体)

For example in Java:

String str = "Hello";  //statically typed as string
str = 5;               //would throw an error since java is statically typed

Whereas in a dynamically typed language the type is dynamic, meaning after you set a variable to a type, you CAN change it. That is because typing is associated with the value rather than the variable. (动态类型语言中类型与值是一个整体)

For example in Python:

str = "Hello" # it is a string
str = 5       # now it is an integer; perfectly OK

强/弱类型语言

  • Strong/Weak typing is about how strictly types are distinguished (e.g. whether the language tries to do implicit conversion from strings to numbers).

For example in Python:

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

whereas in PHP:

$str = 5 + "hello"; // equals 5 because "hello" is implicitly casted to 0 
// PHP is weakly typed, thus is a very forgiving language.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值