当我们说一种语言是动态类型和静态类型时,这意味着什么?
当前回答
静态类型语言:每个变量和表达式在编译时就已经知道了。
(int;A在运行时只能接受整型值)
例如:C, c++, Java
动态类型语言:变量可以在运行时接收不同的值,它们的类型在运行时定义。
(var;A可以在运行时取任何类型的值)
例如:Ruby, 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等。
静态类型语言:每个变量和表达式在编译时就已经知道了。
(int;A在运行时只能接受整型值)
例如:C, c++, Java
动态类型语言:变量可以在运行时接收不同的值,它们的类型在运行时定义。
(var;A可以在运行时取任何类型的值)
例如:Ruby, Python。
静态类型语言在编译时进行类型检查,并且类型不能更改。(不要用类型转换注释,会创建一个新的变量/引用)。
动态类型语言在运行时进行类型检查,变量的类型可以在运行时更改。
下面是一个对比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)