静态/动态类型和强/弱类型之间的区别是什么?


当前回答

简单地说:在静态类型语言中,类型是静态的,这意味着一旦将变量设置为类型,就不能更改它。这是因为类型是与变量而不是它所引用的值相关联的。

例如,在Java中:

String str = "Hello";  //statically typed as string
str = 5;               //would throw an error since java is statically typed

而在动态类型语言中,类型是动态的,这意味着在将变量设置为类型之后,您可以更改它。这是因为类型与值而不是变量相关联。

例如,在Python中:

str = "Hello" # it is a string
str = 5       # now it is an integer; perfectly OK

另一方面,语言中的强/弱类型与隐式类型转换有关(部分取自@Dario的回答):

例如,在Python中:

str = 5 + "hello" 
# would throw an error since it does not want to cast one type to the other implicitly. 

而在PHP中:

$str = 5 + "hello"; // equals 5 because "hello" is implicitly casted to 0 
// PHP is weakly typed, thus is a very forgiving language.

静态类型允许在编译时检查类型正确性。静态类型的语言通常是编译的,动态类型的语言是解释的。因此,动态类型语言可以在运行时检查类型。

其他回答

我认为其他同事做得很好,特别是解释了静态类型和动态类型的区别。但就强类型和弱类型而言,应该说都有 不同的理解/观点。

这里有两个例子:

有人说Haskell是强类型的,因为不允许进行任何类型转换。 其他人(例如Dario的观点)说,允许有意地从字符串隐式转换为数字的语言是弱类型的,但甚至其他人称之为鸭子类型。

这两种说法强调的不是类型系统的两个极端,而是完全不同的方面。因此,我同意Ramsey先生的观点,不要用“强”和“弱”来区分类型系统。

今天在研究这个主题时,我看到了这篇很棒的文章http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html,它为我理清了很多事情,我认为它可能会为上面的一些伟大的答案补充一些东西。

强和弱打字:

可能类型系统最常见的分类方法是“强” 或“弱”。这是不幸的,因为这些话几乎没有 完全没有意义。在有限的范围内,比较两者是可能的 具有非常相似类型系统的语言,并指定其中一个为具有 这两个系统中较强的一个。除此之外,这些话毫无意义 在所有。

静态和动态类型

This is very nearly the only common classification of type systems that has real meaning. As a matter of fact, it's significance is frequently under-estimated [...] Dynamic and static type systems are two completely different things, whose goals happen to partially overlap. A static type system is a mechanism by which a compiler examines source code and assigns labels (called "types") to pieces of the syntax, and then uses them to infer something about the program's behavior. A dynamic type system is a mechanism by which a compiler generates code to keep track of the sort of data (coincidentally, also called its "type") used by the program. The use of the same word "type" in each of these two systems is, of course, not really entirely coincidental; yet it is best understood as having a sort of weak historical significance. Great confusion results from trying to find a world view in which "type" really means the same thing in both systems. It doesn't.

显式或隐式类型:

When these terms are used, they refer to the extent to which a compiler will reason about the static types of parts of a program. All programming languages have some form of reasoning about types. Some have more than others. ML and Haskell have implicit types, in that no (or very few, depending on the language and extensions in use) type declarations are needed. Java and Ada have very explicit types, and one is constantly declaring the types of things. All of the above have (relatively, compared to C and C++, for example) strong static type systems.

简单地说:在静态类型语言中,类型是静态的,这意味着一旦将变量设置为类型,就不能更改它。这是因为类型是与变量而不是它所引用的值相关联的。

例如,在Java中:

String str = "Hello";  //statically typed as string
str = 5;               //would throw an error since java is statically typed

而在动态类型语言中,类型是动态的,这意味着在将变量设置为类型之后,您可以更改它。这是因为类型与值而不是变量相关联。

例如,在Python中:

str = "Hello" # it is a string
str = 5       # now it is an integer; perfectly OK

另一方面,语言中的强/弱类型与隐式类型转换有关(部分取自@Dario的回答):

例如,在Python中:

str = 5 + "hello" 
# would throw an error since it does not want to cast one type to the other implicitly. 

而在PHP中:

$str = 5 + "hello"; // equals 5 because "hello" is implicitly casted to 0 
// PHP is weakly typed, thus is a very forgiving language.

静态类型允许在编译时检查类型正确性。静态类型的语言通常是编译的,动态类型的语言是解释的。因此,动态类型语言可以在运行时检查类型。

摘自Addison Wesley,《面向对象的分析与应用设计》,第3期,第66页:

The concepts of strong and weak typing and static and dynamic typing are entirely different. Strong and weak typing refers to type consistency, whereas static and dynamic typing refers to the time when names are bound to types. Static typing (also known as static binding or early binding) means that the types of all variables and expressions are fixed at the time of compilation; dynamic typing (also known as late binding) means that the types of all variables and expressions are not known until runtime. A language may be both strongly and statically typed (Ada), strongly typed yet supportive of dynamic typing (C++, Java), or untyped yet supportive of dynamic typing (Smalltalk).

You have discovered a soft spot in the terminology that amateurs use to talk about programming languages. Don't use the terms "strong" and "weak" typing, because they don't have a universally agreed on technical meaning. By contrast, static typing means that programs are checked before being executed, and a program might be rejected before it starts. Dynamic typing means that the types of values are checked during execution, and a poorly typed operation might cause the program to halt or otherwise signal an error at run time. A primary reason for static typing is to rule out programs that might have such "dynamic type errors".

Strong typing generally means that there are no loopholes in the type system, whereas weak typing means the type system can be subverted (invalidating any guarantees). The terms are often used incorrectly to mean static and dynamic typing. To see the difference, think of C: the language is type-checked at compile time (static typing), but there are plenty of loopholes; you can pretty much cast a value of any type to another type of the same size---in particular, you can cast pointer types freely. Pascal was a language that was intended to be strongly typed but famously had an unforeseen loophole: a variant record with no tag.

随着时间的推移,强类型语言的实现经常会出现漏洞,通常是为了让运行时系统的一部分可以用高级语言实现。例如,Objective Caml有一个叫做Obj的函数。Magic,它具有简单地返回其参数的运行时效果,但在编译时它将任何类型的值转换为任何其他类型的值。我最喜欢的例子是Modula-3,它的设计者称其类型转换结构为漏洞。

话虽如此,你不能指望任何两个人以完全相同的方式使用“强”和“弱”这个词。所以要避开它们。