当我们说一种语言是动态类型和静态类型时,这意味着什么?


http://en.wikipedia.org/wiki/Type_system

Static typing A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. In static typing, types are associated with variables not values. Statically typed languages include Ada, C, C++, C#, JADE, Java, Fortran, Haskell, ML, Pascal, Perl (with respect to distinguishing scalars, arrays, hashes and subroutines) and Scala. Static typing is a limited form of program verification (see type safety): accordingly, it allows many type errors to be caught early in the development cycle. Static type checkers evaluate only the type information that can be determined at compile time, but are able to verify that the checked conditions hold for all possible executions of the program, which eliminates the need to repeat type checks every time the program is executed. Program execution may also be made more efficient (i.e. faster or taking reduced memory) by omitting runtime type checks and enabling other optimizations. Because they evaluate type information during compilation, and therefore lack type information that is only available at run-time, static type checkers are conservative. They will reject some programs that may be well-behaved at run-time, but that cannot be statically determined to be well-typed. For example, even if an expression always evaluates to true at run-time, a program containing the code if <complex test> then 42 else <type error> will be rejected as ill-typed, because a static analysis cannot determine that the else branch won't be taken.[1] The conservative behaviour of static type checkers is advantageous when evaluates to false infrequently: A static type checker can detect type errors in rarely used code paths. Without static type checking, even code coverage tests with 100% code coverage may be unable to find such type errors. Code coverage tests may fail to detect such type errors because the combination of all places where values are created and all places where a certain value is used must be taken into account. The most widely used statically typed languages are not formally type safe. They have "loopholes" in the programming language specification enabling programmers to write code that circumvents the verification performed by a static type checker and so address a wider range of problems. For example, Java and most C-style languages have type punning, and Haskell has such features as unsafePerformIO: such operations may be unsafe at runtime, in that they can cause unwanted behaviour due to incorrect typing of values when the program runs. Dynamic typing A programming language is said to be dynamically typed, or just 'dynamic', when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing, types are associated with values not variables. Dynamically typed languages include Groovy, JavaScript, Lisp, Lua, Objective-C, Perl (with respect to user-defined types but not built-in types), PHP, Prolog, Python, Ruby, Smalltalk and Tcl. Compared to static typing, dynamic typing can be more flexible (e.g. by allowing programs to generate types and functionality based on run-time data), though at the expense of fewer a priori guarantees. This is because a dynamically typed language accepts and attempts to execute some programs which may be ruled as invalid by a static type checker. Dynamic typing may result in runtime type errors—that is, at runtime, a value may have an unexpected type, and an operation nonsensical for that type is applied. This operation may occur long after the place where the programming mistake was made—that is, the place where the wrong type of data passed into a place it should not have. This makes the bug difficult to locate. Dynamically typed language systems, compared to their statically typed cousins, make fewer "compile-time" checks on the source code (but will check, for example, that the program is syntactically correct). Run-time checks can potentially be more sophisticated, since they can use dynamic information as well as any information that was present during compilation. On the other hand, runtime checks only assert that conditions hold in a particular execution of the program, and these checks are repeated for every execution of the program. Development in dynamically typed languages is often supported by programming practices such as unit testing. Testing is a key practice in professional software development, and is particularly important in dynamically typed languages. In practice, the testing done to ensure correct program operation can detect a much wider range of errors than static type-checking, but conversely cannot search as comprehensively for the errors that both testing and static type checking are able to detect. Testing can be incorporated into the software build cycle, in which case it can be thought of as a "compile-time" check, in that the program user will not have to manually run such tests. References Pierce, Benjamin (2002). Types and Programming Languages. MIT Press. ISBN 0-262-16209-1.


静态类型语言

如果一个变量的类型在编译时已知,那么该语言就是静态类型的。对于某些语言,这意味着作为程序员的你必须指定每个变量的类型;其他语言(例如:Java, C, c++)提供了某种形式的类型推断,类型系统推断变量类型的能力(例如:OCaml, Haskell, Scala, Kotlin)。

这样做的主要优点是编译器可以完成所有类型的检查,因此在非常早期的阶段就可以捕捉到许多微不足道的错误。

例如:C, c++, Java, Rust, Go, Scala

动态类型语言

如果一种语言的类型与运行时值相关联,而不是命名变量/字段等,则该语言是动态类型的。这意味着作为程序员,您可以编写得更快一些,因为您不必每次都指定类型(除非使用带有类型推断的静态类型语言)。

例如:Perl, Ruby, Python, PHP, JavaScript, Erlang

大多数脚本语言都有这个特性,因为没有编译器来进行静态类型检查,但您可能会发现自己在搜索由于解释器错误解释变量类型而导致的错误。幸运的是,脚本往往很小,所以bug没有那么多藏身之处。

大多数动态类型语言允许您提供类型信息,但不要求提供类型信息。目前正在开发的一种语言Rascal采用了一种混合方法,允许函数内的动态类型,但对函数签名强制执行静态类型。


类型检查是验证和强制执行类型约束的过程。

静态类型编程语言在编译时进行类型检查。 例如:Java, C, c++。 动态类型编程语言在运行时进行类型检查。 例子: Perl, Ruby, Python, PHP, JavaScript。


静态类型: Java和Scala等语言是静态类型的。

在代码中使用变量之前,必须对变量进行定义和初始化。

对于exp . int x;X = 10;

System.out.println (x);

动态类型: Perl是一种动态类型语言。

变量在代码中使用之前不需要初始化。

y = 10;在后面的代码中使用这个变量


下面是一个对比Python(动态类型)和Go(静态类型)如何处理类型错误的例子:

def silly(a):
    if a > 0:
        print 'Hi'
    else:
        print 5 + '3'

Python在运行时执行类型检查,因此:

silly(2)

运行完全正常,并产生预期的输出Hi。只有当有问题的行被击中时才会引发错误:

silly(-1)

生产

不支持'int'和'str'的操作数类型

因为相关的行已经被执行了。

另一方面,Go在编译时进行类型检查:

package main

import ("fmt"
)

func silly(a int) {
    if (a > 0) {
        fmt.Println("Hi")
    } else {
        fmt.Println("3" + 5)
    }
}

func main() {
    silly(2)
}

上述文件将无法编译,并出现以下错误:

无效的操作:"3" + 5(不匹配的类型字符串和int)


不幸的是,术语“动态类型”具有误导性。所有语言都是静态类型的,类型是表达式的属性(而不是一些人认为的值的属性)。然而,有些语言只有一种类型。这些被称为单一类型语言。这种语言的一个例子是无类型lambda演算。

在无类型lambda演算中,所有的项都是lambda项,对一个项执行的唯一操作是将它应用到另一个项。因此,所有的操作总是导致无限递归或lambda项,但永远不会发出错误信号。

However, were we to augment the untyped lambda calculus with primitive numbers and arithmetic operations, then we could perform nonsensical operations, such adding two lambda terms together: (λx.x) + (λy.y). One could argue that the only sane thing to do is to signal an error when this happens, but to be able to do this, each value has to be tagged with an indicator that indicates whether the term is a lambda term or a number. The addition operator will then check that indeed both arguments are tagged as numbers, and if they aren't, signal an error. Note that these tags are not types, because types are properties of programs, not of values produced by those programs.

这样做的单一类型语言称为动态类型语言。

JavaScript、Python和Ruby等语言都是单一类型的。同样,JavaScript中的typeof操作符和Python中的type函数的名称具有误导性;它们返回与操作数相关的标记,而不是操作数的类型。类似地,c++中的dynamic_cast和Java中的instanceof不做类型检查。


简单地说:在静态类型语言中,变量的类型是静态的,这意味着一旦将变量设置为类型,就不能更改它。这是因为类型是与变量而不是它所引用的值相关联的。

例如,在Java中:

String str = "Hello";  // variable str statically typed as string
str = 5;               // would throw an error since str is
                       // supposed to be a string only

另一方面:在动态类型语言中,变量的类型是动态的,这意味着在你将变量设置为类型后,你可以更改它。这是因为输入与它假设的值相关,而不是变量本身。

例如,在Python中:

some_str = "Hello"  # variable some_str is linked to a string value
some_str = 5        # now it is linked to an integer value; perfectly OK

因此,最好将动态类型语言中的变量视为指向类型值的泛型指针。

总而言之,类型描述(或应该描述)语言中的变量,而不是语言本身。恕我直言,它本可以更好地用作带有静态类型变量的语言,而不是带有动态类型变量的语言。

静态类型语言通常是编译语言,因此,编译器检查类型(这很有意义,对吧?因为类型不允许稍后在运行时更改)。

动态类型语言通常是解释型的,因此类型检查(如果有的话)发生在使用它们时的运行时。这当然会带来一些性能损失,也是动态语言(例如python、ruby、php)伸缩性不如类型化语言(java、c#等)的原因之一。从另一个角度来看,静态类型语言的启动成本更高:通常会让你编写更多、更难的代码。但这在以后是有回报的。

好在双方都在借鉴对方的特点。类型语言融合了更多的动态特性,例如c#中的泛型和动态库,而动态语言则包含了更多的类型检查,例如python中的类型注释,或PHP的HACK变体,这些通常不是语言的核心,可按需使用。

在技术选择方面,任何一方都没有内在的优势。这只是一个偏好的问题,你想要更多的控制或灵活性。只要为工作选择合适的工具,并确保在考虑转换之前检查相反的可用工具。


静态类型语言在编译时进行类型检查,并且类型不能更改。(不要用类型转换注释,会创建一个新的变量/引用)。

动态类型语言在运行时进行类型检查,变量的类型可以在运行时更改。


在静态类型语言中,变量与编译时已知的类型相关联,并且该类型在整个程序执行过程中保持不变。同样,变量只能被赋值为已知/指定类型的实例。 在动态类型语言中,变量没有类型,它在执行期间的值可以是任何形状和形式的任何东西。


甜蜜和简单的定义,但符合需求: 静态类型语言将类型绑定到整个作用域的变量(Seg: SCALA) 动态类型语言将类型绑定到变量引用的实际值。


静态类型语言:每个变量和表达式在编译时就已经知道了。

(int;A在运行时只能接受整型值)

例如:C, c++, Java

动态类型语言:变量可以在运行时接收不同的值,它们的类型在运行时定义。

(var;A可以在运行时取任何类型的值)

例如:Ruby, Python。


编译vs.解释

“当源代码被翻译时”

源代码:原始代码(通常由人输入计算机) 翻译:将源代码转换为计算机可以阅读的内容(即机器代码) 运行时间:程序执行命令的时间(编译后,如果编译) 编译语言:在运行时之前翻译的代码 解释语言:在执行过程中动态翻译的代码


打字

当类型被检查时

5 + '3'是强类型语言(如Go和Python)中类型错误的一个例子,因为它们不允许“类型强制”——>值在某些上下文中改变类型的能力,例如合并两种类型。弱类型语言,如JavaScript,不会抛出类型错误(结果为'53')。

静态:运行前检查的类型 动态:在执行期间动态检查类型


“静态和编译”和“动态和解释”的定义非常相似……但是记住,当类型被检查时。“当源代码被翻译时”。

无论语言是编译的还是解释的,你都会得到相同的类型错误!你需要从概念上区分这些术语。


Python示例

动态的,解释

def silly(a):
    if a > 0:
        print 'Hi'
    else:
        print 5 + '3'

silly(2)

因为Python是解释性的和动态类型的,所以它只翻译和类型检查它所执行的代码。else块永远不会执行,所以5 + '3'甚至永远不会被查看!

如果它是静态类型的呢?

甚至在代码运行之前就会抛出类型错误。即使它是解释的,它仍然在运行时之前执行类型检查。

如果它是编译的呢?

else块将在运行时之前被转换/查看,但因为它是动态类型的,所以不会抛出错误!动态类型语言直到执行才检查类型,而这一行永远不会执行。


去的例子

静态的,编译

package main

import ("fmt"
)

func silly(a int) {
  if (a > 0) {
      fmt.Println("Hi")
  } else {
      fmt.Println("3" + 5)
  }
}

func main() {
  silly(2)
}

类型在运行前检查(静态),类型错误立即被捕获!如果是解释类型,则在运行时之前仍将检查类型,结果相同。如果它是动态的,即使在编译期间查看代码,它也不会抛出任何错误。


性能

如果编译语言是静态类型(而不是动态类型),那么它在运行时的性能会更好;类型知识允许机器代码优化。

静态类型语言本质上在运行时具有更好的性能,因为在执行时不需要动态地检查类型(它在运行前检查)。

类似地,编译语言在运行时速度更快,因为代码已经被翻译,而不需要动态地“解释”/翻译。

注意,编译语言和静态类型语言在分别进行翻译和类型检查之前都会有一个延迟。


更多的差异

静态类型在早期捕获错误,而不是在执行期间发现错误(对长程序特别有用)。它更“严格”,因为它不允许程序中的任何地方出现类型错误,并且经常防止变量改变类型,这进一步防止了意外错误。

num = 2
num = '3' // ERROR

动态类型更灵活,这一点受到一些人的赞赏。它通常允许变量更改类型,这可能导致意外错误。


动态类型语言有助于快速构建算法概念原型,而不需要考虑需要使用什么变量类型(这在静态类型语言中是必要的)。


静态类型语言(如c++、Java)和动态类型语言(如Python)的区别仅仅在于变量类型的执行。 静态类型语言对变量有静态数据类型,这里在编译期间检查数据类型,因此调试要简单得多……而动态类型语言则不这样做,它会检查执行程序的数据类型,因此调试有点困难。

此外,它们有一个非常小的区别,可以与强类型和弱类型语言相关联。强类型语言不允许将一种类型用作另一种类型。C和c++…而弱类型语言允许例如python


静态类型语言(编译器解析方法调用和编译引用):

通常表现更好 更快的编译错误反馈 更好的IDE支持 不适合使用未定义的数据格式 当没有定义模型时,很难开始开发 更长的编译时间 在很多情况下需要编写更多的代码

动态类型语言(在运行程序中做出的决定):

较低的性能 更快的发展 有些bug可能只在稍后的运行时才会被检测到 适用于未定义的数据格式(元编程)


静态类型

在运行前检查类型,以便更早地捕获错误。

Examples = c++

动态类型

在执行期间检查类型。

示例= Python


静态类型:在编译时执行的类型检查。

静态类型语言的真正含义是:

必须指定变量的类型 变量只能引用特定类型的对象* 值的类型检查将在编译时执行,任何类型检查都将在此时报告 将在编译时分配内存来存储该特定类型的值

静态类型语言的例子有C、c++、Java。

动态类型:在运行时执行的类型检查。

动态类型语言的真正含义是:

不需要指定变量的类型 同一个变量可以引用不同类型的对象

Python、Ruby都是动态类型语言的例子。


*一些对象可以通过类型转换分配给不同类型的变量(在C和c++等语言中非常常见的做法)


允许程序在运行时更改变量类型的动态类型编程。

动态类型语言:Perl、Ruby、Python、PHP、JavaScript、Erlang

静态类型,意味着如果你试图在一个整数变量中存储一个字符串,它将不会接受它。

静态类型语言:C、c++、Java、Rust、Go、Scala、Dart


在编程中,数据类型是一种分类,它告诉变量将持有什么类型的值,以及可以对这些值进行哪些数学、关系和逻辑操作而不会出错。

在每种编程语言中,为了尽量减少出错的机会,类型检查都是在程序执行之前或执行过程中进行的。根据类型检查的时间,编程语言有两种类型:静态类型语言和动态类型语言。

也取决于是否发生隐式类型转换,编程语言有两种类型:强类型语言和弱类型语言。

静态类型:

Type checking is done at compile time In source code, at the time of variable declaration, data type of that variable must be explicitly specified. Because if data type is specified in source code then at compile time that source code will be converted to machine code and type checking can happen Here data type is associated with variable like, int count. And this association is static or fixed If we try to change data type of an already declared variable (int count) by assigning a value of other data type (int count = "Hello") into it, then we will get error If we try to change data type by redeclaring an already declared variable (int count) using other data type (boolean count) then also we will get error

int count;         /* count is int type, association between data type
                      and variable is static or fixed */

count = 10;        // no error 
count = 'Hello';   // error 
boolean count;     // error 

由于类型检查和类型错误检测是在编译时完成的,这就是为什么在运行时不需要进一步的类型检查。因此,程序变得更加优化,结果在更快的执行 如果我们想要更严格的代码,那么选择这种类型的语言是更好的选择 例如:Java, C, c++, Go, Swift等。

动态类型:

Type checking is done at runtime In source code, at the time of variable declaration, no need to explicitly specify data type of that variable. Because during type checking at runtime, the language system determines variable type from data type of the assigned value to that variable Here data type is associated with the value assigned to the variable like, var foo = 10, 10 is a Number so now foo is of Number data type. But this association is dynamic or flexible we can easily change data type of an already declared variable (var foo = 10), by assigning a value of other data type (foo = "Hi") into it, no error we can easily change data type of an already declared variable (var foo = 10), by redeclaring it using value of other data type (var foo = true), no error

var foo;            // without assigned value, variable holds undefined data type 

var foo = 10;       // foo is Number type now, association between data 
                    // type and value is dynamic / flexible 
foo = 'Hi';         // foo is String type now, no error 
var foo = true;     // foo is Boolean type now, no error 

由于类型检查和类型错误检测是在运行时完成的,这就是为什么程序变得不那么优化,导致执行速度变慢。尽管如果它们实现了JIT编译器,这些类型的语言的执行速度会更快 如果我们想要轻松地编写和执行代码,那么这种类型的语言是更好的选择,但在这里我们可能会得到运行时错误 例如:Python, JavaScript, PHP, Ruby等。