当我们说一种语言是动态类型和静态类型时,这意味着什么?
当前回答
静态类型语言(如c++、Java)和动态类型语言(如Python)的区别仅仅在于变量类型的执行。 静态类型语言对变量有静态数据类型,这里在编译期间检查数据类型,因此调试要简单得多……而动态类型语言则不这样做,它会检查执行程序的数据类型,因此调试有点困难。
此外,它们有一个非常小的区别,可以与强类型和弱类型语言相关联。强类型语言不允许将一种类型用作另一种类型。C和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等。
静态类型
在运行前检查类型,以便更早地捕获错误。
Examples = c++
动态类型
在执行期间检查类型。
示例= Python
不幸的是,术语“动态类型”具有误导性。所有语言都是静态类型的,类型是表达式的属性(而不是一些人认为的值的属性)。然而,有些语言只有一种类型。这些被称为单一类型语言。这种语言的一个例子是无类型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不做类型检查。
甜蜜和简单的定义,但符合需求: 静态类型语言将类型绑定到整个作用域的变量(Seg: SCALA) 动态类型语言将类型绑定到变量引用的实际值。