静态/动态类型和强/弱类型之间的区别是什么?
当前回答
简单地说:在静态类型语言中,类型是静态的,这意味着一旦将变量设置为类型,就不能更改它。这是因为类型是与变量而不是它所引用的值相关联的。
例如,在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.
静态类型允许在编译时检查类型正确性。静态类型的语言通常是编译的,动态类型的语言是解释的。因此,动态类型语言可以在运行时检查类型。
其他回答
静态v/s动态类型语言
Statically typed languages are those in which type checking is done at the compile time, so this also means that in statically typed languages each variable has a type and it doesn’t change over the course. Now, in contrast, dynamically typed languages are those in which type checking is done at runtime, and there is no type checking at compile time, so this also means that in dynamically typed languages there may or may not be a type associated with a variables, and if a type is associated then it could be a generic type like “var” in JS which hold good for both a string and number. “Implementations of dynamically type-checked languages generally associate each runtime object with a type tag (i.e., a reference to a type) containing its type information. This runtime type information (RTTI) can also be used to implement dynamic dispatch, late binding, down casting, reflection, and similar features.” Even if language is statically typed, still it could have some dynamically typed feature, which basically means that some sort of type checking at runtime as well. This is useful in casting of types. “A number of useful and common programming language features cannot be checked statically, such as down casting. Thus, many languages will have both static and dynamic type checking; the static type checker verifies what it can, and dynamic checks verify the rest.” “Some languages allow writing code that is not type-safe. For example, in C, programmers can freely cast a value between any two types that have the same size.” Advantage of “statically” typed languages are that: Since most of the type checking is done at compile time so interpreter or runtime can run at full speed, without worrying about the types. It leads to lesser number of runtime exception or errors related to type, because most of the type checking is done at compile time. Advantage of “dynamically” typed languages are that: They could help in extremely fast prototyping, since developer need not to understand the type system so dev can loosely create variables and run it, and this leads to very fast prototyping. List of statically and dynamically typed languages: Statically: Java C (C is a statically typed language but lesser “strongly” typed as compared to Java because it allows more implicit conversions) C++ C# Dynamically: PERL PHP Python JavaScript Ruby Type checking is an important security feature. Suppose, there is no type checking, and a method accepts an object of type “BankAccount” which has a method called as “creditAccount(BankAccountDetails)”, now at runtime if there is no type checking then I can pass an object of my own class which has same method “creditAccount(BankAccountDetails)” and it will get executed, considering we are talking about object oriented language because OOP supports “polymorphism” and here what we are discussing is nothing but “polymorphism”. So, basically an object oriented language (which basically means it supports “polymorphism”) which doesn’t have strong type checking can lead to security issues.
强v/s弱类型语言
Strongly typed languages are those in which implicit conversions are not allowed if there is loss of precision. For example, in Java, you can cast an “int to long” because there is no loss of precision but you cannot “implicitly” cast a “long to int” because there would be loss of precision. In contrast, in weakly typed languages, implicit conversions are allowed even if there is loss of precision. I think dynamically typed language can also be a strongly typed language if “at runtime” it doesn’t allow implicit conversions in which there is loss of precision.
很好的进一步阅读
Type_system Strong_and_weak_typing 类别:Statically_typed_programming_languages 类别:Dynamically_typed_programming_languages
简单地说:在静态类型语言中,类型是静态的,这意味着一旦将变量设置为类型,就不能更改它。这是因为类型是与变量而不是它所引用的值相关联的。
例如,在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.
静态类型允许在编译时检查类型正确性。静态类型的语言通常是编译的,动态类型的语言是解释的。因此,动态类型语言可以在运行时检查类型。
弱类型意味着对象的类型可以根据上下文而改变。例如,在弱类型语言中,字符串“123”如果添加另一个数字,可能会被视为数字123。具有弱类型的语言示例有bash、awk和PHP。
另一种弱类型语言是C语言,其中内存地址上的数据可以通过强制转换被视为不同的类型。
在强类型语言中,对象的类型不会改变——int始终是int,试图将其用作字符串将导致错误。Java和Python都是强类型的。
动态类型和静态类型之间的区别在于类型规则何时被强制执行。在静态类型语言中,每个变量和参数的类型都必须在源代码中声明,并在编译时强制执行。在动态类型语言中,类型只在运行时使用时进行检查。Java是静态类型,Python是动态类型。
然而,界限有时会有点模糊。例如,尽管Java是静态类型的,但每次你使用反射或强制转换(例如,当使用对象容器时),你都将类型检查推迟到运行时。
类似地,大多数强类型语言仍然会在整数和浮点数之间自动转换(在某些语言中是任意精确的bigint)。
静态/动态类型是关于获取类型信息的时间(在编译时或在运行时) 强/弱类型是关于如何严格区分类型(例如,语言是否试图进行从字符串到数字的隐式转换)。
更多详细信息请参见维基页面。
摘自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).