强类型,弱类型,静态类型,动态类型的区别

    xiaoxiao2023-10-09  164

    首先了解一下基本概念

    Program Errors

    trapped errors。导致程序终止执行,如除0,Java中数组越界访问untrapped errors。 出错后继续执行,但可能出现任意行为。如C里的缓冲区溢出、Jump到错误地址

    Forbidden Behaviours

    语言设计时,可以定义一组forbidden behaviors. 它必须包括所有untrapped errors, 但可能包含trapped errors.

    Well behaved、ill behaved

    well behaved: 如果程序执行不可能出现forbidden behaviors, 则为well behaved。ill behaved: 否则为ill behaved。

    有了上面的概念,再讨论强、弱类型,静态、动态类型

    强、弱类型

    强类型strongly typed: 如果一种语言的所有程序都是well behaved——即不可能出现forbidden behaviors,则该语言为strongly typed。 强类型既不会出现出错后继续执行的行为弱类型weakly typed: 否则为weakly typed。比如C语言的缓冲区溢出,属于trapped errors,即属于forbidden behaviors…故C是弱类型

    偏向于不容忍隐式类型转换,弱类型相对于强类型来说类型检查更不严格,比如说允许变量类型的隐式转换,允许强制类型转换等等。强类型语言一般不允许这么做。

    弱类型 容忍类型变化

    > "1"+2 '12'

    强类型

    >>> "1"+2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects

    动态、静态类型

    静态类型 statically: 如果在编译时拒绝ill behaved程序(是Well behaved程序,程序执行不可能出现forbidden behaviors,不可能出现出错后继续执行的行为),则是statically typed;动态类型dynamiclly: 如果在运行时拒绝ill behaviors, 则是dynamiclly typed。

    其中静态类型可以分为两种:

    如果类型是语言语法的一部分,在是explicitly typed显式类型,比如java和c;如果类型通过编译时推导,是implicity typed隐式类型, 比如ML和Haskell

    简单地说,就是在声明了一个变量之后,不能改变它的类型的语言,是静态语言;能够随时改变它的类型的语言,是动态语言。因为动态语言的特性,一般需要运行时虚拟机支持。

    动态类型

    >>> a = 1 >>> type(a) <type 'int'> >>> a = "s" >>> type(a) <type 'str'>

    静态类型

    Prelude> let a = "123" :: Int <interactive>:2:9: Couldn't match expected type `Int' with actual type `[Char]' In the expression: "123" :: Int In an equation for `a': a = "123" :: Int

    一些图示

    最新回复(0)