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


当前回答

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

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

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

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

其他回答

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

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

对于exp . int x;X = 10;

System.out.println (x);

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

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

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

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

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

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

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

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

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

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

静态类型:

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