当我们说一种语言是动态类型和静态类型时,这意味着什么?
当前回答
动态类型语言有助于快速构建算法概念原型,而不需要考虑需要使用什么变量类型(这在静态类型语言中是必要的)。
其他回答
静态类型语言
如果一个变量的类型在编译时已知,那么该语言就是静态类型的。对于某些语言,这意味着作为程序员的你必须指定每个变量的类型;其他语言(例如:Java, C, c++)提供了某种形式的类型推断,类型系统推断变量类型的能力(例如:OCaml, Haskell, Scala, Kotlin)。
这样做的主要优点是编译器可以完成所有类型的检查,因此在非常早期的阶段就可以捕捉到许多微不足道的错误。
例如:C, c++, Java, Rust, Go, Scala
动态类型语言
如果一种语言的类型与运行时值相关联,而不是命名变量/字段等,则该语言是动态类型的。这意味着作为程序员,您可以编写得更快一些,因为您不必每次都指定类型(除非使用带有类型推断的静态类型语言)。
例如:Perl, Ruby, Python, PHP, JavaScript, Erlang
大多数脚本语言都有这个特性,因为没有编译器来进行静态类型检查,但您可能会发现自己在搜索由于解释器错误解释变量类型而导致的错误。幸运的是,脚本往往很小,所以bug没有那么多藏身之处。
大多数动态类型语言允许您提供类型信息,但不要求提供类型信息。目前正在开发的一种语言Rascal采用了一种混合方法,允许函数内的动态类型,但对函数签名强制执行静态类型。
在静态类型语言中,变量与编译时已知的类型相关联,并且该类型在整个程序执行过程中保持不变。同样,变量只能被赋值为已知/指定类型的实例。 在动态类型语言中,变量没有类型,它在执行期间的值可以是任何形状和形式的任何东西。
在编程中,数据类型是一种分类,它告诉变量将持有什么类型的值,以及可以对这些值进行哪些数学、关系和逻辑操作而不会出错。
在每种编程语言中,为了尽量减少出错的机会,类型检查都是在程序执行之前或执行过程中进行的。根据类型检查的时间,编程语言有两种类型:静态类型语言和动态类型语言。
也取决于是否发生隐式类型转换,编程语言有两种类型:强类型语言和弱类型语言。
静态类型:
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等。
允许程序在运行时更改变量类型的动态类型编程。
动态类型语言:Perl、Ruby、Python、PHP、JavaScript、Erlang
静态类型,意味着如果你试图在一个整数变量中存储一个字符串,它将不会接受它。
静态类型语言:C、c++、Java、Rust、Go、Scala、Dart
静态类型语言:每个变量和表达式在编译时就已经知道了。
(int;A在运行时只能接受整型值)
例如:C, c++, Java
动态类型语言:变量可以在运行时接收不同的值,它们的类型在运行时定义。
(var;A可以在运行时取任何类型的值)
例如:Ruby, Python。