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


当前回答

静态类型

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

Examples = c++

动态类型

在执行期间检查类型。

示例= Python

其他回答

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

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

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

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

静态类型:

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等。

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

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

编译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

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

下面是一个对比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)