静态/动态与强/弱

发布于 2024-08-22 21:27:40 字数 26 浏览 4 评论 0原文

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

What is the difference between Static/Dynamic and Strong/Weak typing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

静水深流 2024-08-29 21:27:40
  • 静态/动态类型何时获取类型信息有关(在编译时或运行时)

  • 强/弱类型是关于严格程度类型是区分的(例如,语言是否尝试从字符串到数字进行隐式转换)。

有关更多详细信息,请参阅wiki 页面

  • Static/Dynamic Typing is about when type information is acquired (Either at compile time or at runtime)

  • Strong/Weak Typing is about how strictly types are distinguished (e.g. whether the language tries to do an implicit conversion from strings to numbers).

See the wiki-page for more detailed information.

盛夏已如深秋| 2024-08-29 21:27:40

您发现了业余爱好者用来谈论编程语言的术语的一个弱点。
不要使用术语“强”和“弱”类型,因为它们没有普遍认可的技术含义。相比之下,静态类型意味着程序在执行之前会被检查,并且程序可能在启动之前被拒绝。 动态类型意味着的类型在执行期间被检查,并且类型错误的操作可能会导致程序停止或以其他方式在运行时发出错误信号。静态类型的主要原因是排除可能存在此类“动态类型错误”的程序。

强类型通常意味着类型系统没有漏洞,而弱类型意味着类型系统可以被破坏(使任何保证失效) 。这些术语经常被错误地用来表示静态类型和动态类型。
要了解其中的差异,请考虑 C:该语言在编译时进行类型检查(静态类型),但存在大量漏洞;您几乎可以将任何类型的值转换为相同大小的另一种类型——特别是,您可以自由地转换指针类型。 Pascal 是一种旨在强类型的语言,但众所周知,它有一个不可预见的漏洞:没有标签的变体记录。

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

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

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.

Implementations of strongly typed languages often acquire loopholes over time, usually so that part of the run-time system can be implemented in the high-level language. For example, Objective Caml has a function called Obj.magic which has the run-time effect of simply returning its argument, but at compile time it converts a value of any type to one of any other type. My favorite example is Modula-3, whose designers called their type-casting construct LOOPHOLE.

Having said that, you can't count on any two people using the words "strong" and "weak" in exactly the same way. So avoid them.

万人眼中万个我 2024-08-29 21:27:40

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

例如,在 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.

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

Simply put it this way: in a statically typed language the type is static, meaning once you set a variable to a type, you CANNOT change it. That is because typing is associated with the variable rather than the value it refers to.

For example in Java:

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

Whereas in a dynamically typed language the type is dynamic, meaning after you set a variable to a type, you CAN change it. That is because typing is associated with the value rather than the variable.

For example in Python:

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

On the other hand, the strong/weak typing in a language is related to implicit type conversions (partly taken from @Dario's answer):

For example in Python:

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

whereas in PHP:

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

Static typing allows for checking type correctness at compile time. Statically typed languages are usually compiled, and dynamically typed languages are interpreted. Therefore, dynamicly typed languages can check typing at run time.

执手闯天涯 2024-08-29 21:27:40

弱类型意味着对象的类型可以根据上下文而改变。例如,在弱类型语言中,如果向字符串“123”添加另一个数字,则该字符串可能会被视为数字 123。弱类型语言的例子有 bash、awk 和 PHP。

另一种弱类型语言是 C,其中内存地址处的数据可以通过强制转换被视为不同的类型。

在强类型语言中,对象的类型不会改变 - int 始终是 int,尝试将其用作字符串将导致错误。 Java 和 Python 都是强类型的。

动态类型和静态类型之间的区别在于强制执行类型规则的时间。在静态类型语言中,每个变量和参数的类型都必须在源代码中声明,并在编译时强制执行。在动态类型语言中,仅在运行时使用类型时才检查类型。所以Java是静态类型的,而Python是动态类型的。

然而,界限有时可能有点模糊。例如,尽管 Java 是静态类型的,但每次使用反射或强制类型转换(例如,使用对象容器时),都会将类型检查推迟到运行时。

类似地,大多数强类型语言仍然会自动在整数和浮点数之间进行转换(在某些语言中是任意精度的 BigInt)。

Weak typing means that the type of an object can change depending on context. For example in a weakly typed language the string "123" may be treated as the number 123 if you add another number to it. Examples of languages with weak typing are bash, awk and PHP.

Another kind of weakly typed language is C, where the data at a memory address can be treated as a different type by casting.

In a strongly typed language the type of an object does not change - an int is always an int and trying to use it as a string will result in an error. Both Java and Python are strongly typed.

The difference between dynamic and static typing is when the type rules are enforced. In a statically typed language the type of every variable and parameter must be declared in the source and is enforced at compile time. In a dynamically typed language the types are only checked when they are used at runtime. So Java is statically typed and Python is dynamically typed.

However the boundaries can be a little blurry at times. For example although Java is statically typed, every time you use reflection or a cast (e.g. when using containers of Objects) they you are deferring the type check to runtime.

Similarly most strongly typed languages will still automatically convert between integers and floats (and in some languages abitrary precision BigInts).

才能让你更想念 2024-08-29 21:27:40

今天研究这个主题时,我发现了这篇很棒的文章 http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html 它为我澄清了很多事情,我想它可能会增加上面一些很好的答案。

强类型和弱类型:

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

静态和动态类型

这几乎是类型系统唯一常见的分类
这具有真正的意义。其实它的意义在于
经常被低估[...]动态和静态类型系统
两个完全不同的事情,其目标部分发生
重叠。

静态类型系统是编译器检查的一种机制
源代码并将标签(称为“类型”)分配给各个部分
语法,然后使用它们来推断有关程序的某些信息
行为。动态类型系统是编译器使用的一种机制
生成代码来跟踪数据的排序(巧合的是,也
称为程序使用的“类型”)。使用同一个词
当然,这两个系统中的“类型”并不完全是
巧合;但最好将其理解为具有某种弱点
历史意义。试图找到一个
世界观中“类型”在两者中实际上意味着同一件事
系统。事实并非如此。

显式/隐式类型:

当使用这些术语时,它们指的是
编译器将推断程序各部分的静态类型。全部
编程语言对类型有某种形式的推理。一些
比别人拥有更多。 ML 和 Haskell 具有隐式类型,因为没有
(或很少,取决于所使用的语言和扩展)类型
需要声明。 Java 和 Ada 具有非常明确的类型,并且
人们不断地宣告事物的类型。以上都有
(相对地,例如与 C 和 C++ 相比)强静态类型
系统。

Today researching about this subject I came across this great article http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html It cleared up a lot of things for me and I thought It may add to some of the great answers above.

Strong and Weak Typing:

Probably the most common way type systems are classified is "strong"
or "weak." This is unfortunate, since these words have nearly no
meaning at all. It is, to a limited extent, possible to compare two
languages with very similar type systems, and designate one as having
the stronger of those two systems. Beyond that, the words mean nothing
at all.

Static and Dynamic Types

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.

Explicit/Implicit Types:

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.

沙与沫 2024-08-29 21:27:40

从 Scott 的编程语言语用学,第 3 版第 291 页,我们有

类型检查是确保程序遵守
语言的类型兼容性规则。已知违反规则
作为类型冲突。如果一种语言满足以下条件,则该语言被称为强类型
以语言实现可以强制执行的方式禁止
将任何操作应用于任何不打算用于的对象
支持该操作。如果一种语言被称为静态类型
它是强类型的,可以在编译时执行类型检查
时间。从最严格的意义上来说,很少有语言是静态的
键入。在实践中,该术语经常应用于以下语言:
大多数类型检查可以在编译时执行,其余的可以
在运行时执行。

一些示例:Ada 是强类型的,并且在大多数情况下
静态类型(必须在运行时检查某些类型约束
时间)。 Pascal 实现还可以完成大部分类型检查
在编译时,尽管该语言不是很强类型:
未标记的变体记录(将在第 7.3.4 节中讨论)是其
只是漏洞。 C89 的强类型明显强于它的
前身方言,但类型仍然明显不如以前的方言
帕斯卡.它的漏洞包括联合体、带有可变数量的子程序
参数,以及指针和数组的互操作性(
第 7.7.1 节中讨论)。 C 的实现很少检查
运行时的任何事情。

动态(运行时)类型检查是后期绑定的一种形式,并且倾向于
在将其他问题延迟到运行时的语言中可以找到,例如
出色地。 Lisp 和 Smalltalk 是动态(尽管是强)类型的。最多
脚本语言也是动态类型的;一些(例如,Python 和
Ruby)是强类型的。具有动态作用域的语言通常是
动态类型(或根本不类型):如果编译器不能
识别名称所指的对象,它通常无法确定
对象的类型。

所以简单来说,静态/动态类型指的是类型检查发生的时间:静态类型的编译时间,动态语言的运行时间。同样,强/弱类型指的是一种语言在强制执行其类型系统方面的积极程度。

我试图将斯科特的描述翻译成一个漂亮的图表,我将其发布在下面。

静态/动态 - 强/弱打字平面

From Scott's Programming Language Pragmatics, 3rd edition page 291, we have

Type checking is the process of ensuring that a program obeys the
language’s type compatibility rules. A violation of the rules is known
as a type clash. A language is said to be strongly typed if it
prohibits, in a way that the language implementation can enforce, the
application of any operation to any object that is not intended to
support that operation. A language is said to be statically typed if
it is strongly typed and type checking can be performed at compile
time. In the strictest sense of the term, few languages are statically
typed. In practice, the termis often applied to languages in which
most type checking can be performed at compile time, and the rest can
be performed at run time.

A few examples: Ada is strongly typed, and for the most part
statically typed (certain type constraints must be checked at run
time). A Pascal implementation can also do most of its type checking
at compile time, though the language is not quite strongly typed:
untagged variant records (to be discussed in Section 7.3.4) are its
only loophole. C89 is significantly more strongly typed than its
predecessor dialects, but still significantly less strongly typed than
Pascal. Its loopholes include unions, subroutineswith variable numbers
of parameters, and the interoperability of pointers and arrays (to be
discussed in Section 7.7.1). Implementations of C rarely check
anything at run time.

Dynamic (run-time) type checking is a form of late binding, and tends
to be found in languages that delay other issues until run time as
well. Lisp and Smalltalk are dynamically (though strongly) typed. Most
scripting languages are also dynamically typed; some (e.g., Python and
Ruby) are strongly typed. Languages with dynamic scoping are generally
dynamically typed (or not typed at all): if the compiler can’t
identify the object to which a name refers, it usually can’t determine
the type of the object either.

So in simple terms, static/dynamic typing refers to the time when type checking occurs: compile time for static typing, and run time for dynamic languages. Likewise, strong/weak typing refers to how aggressive a language is in enforcing its type system.

I've tried to translate Scott's description into a nice diagram, which I've posted below.

The Static/Dynamic - Strong/Weak Typing Plane

清醇 2024-08-29 21:27:40

静态与动态类型语言

  • 静态类型语言是在编译时完成类型检查的语言,因此这也意味着在静态类型语言中每个变量都有一个类型并且在整个过程中不会改变。 相比之下,动态类型语言是那些在运行时进行类型检查的语言,并且在编译时没有类型检查,因此这也意味着在动态类型语言中可能有也可能没有是与变量关联的类型,如果关联了类型,那么它可以是通用类型,例如 JS 中的“var”,它适用于字符串和数字。
    • “动态类型检查语言的实现通常将每个运行时对象与包含其类型信息的类型标记(即对类型的引用)相关联。此运行时类型信息 (RTTI) 还可用于实现动态调度、后期绑定、向下转换、反射和类似功能。”
  • 即使语言是静态类型的,它仍然可以具有一些动态类型的功能,这基本上意味着在运行时也进行某种类型检查。这在类型转换时很有用。
    • “许多有用且常见的编程语言功能无法静态检查,例如向下转换。因此,许多语言都具有静态和动态类型检查;静态类型检查器验证它能做什么,动态检查验证其余的。”
  • “有些语言允许编写非类型安全的代码。例如,在 C 中,程序员可以在具有相同大小的任意两种类型之间自由转换值。”
  • “静态”类型语言的优点是:
    • 由于大多数类型检查是在编译时完成的,因此解释器或运行时可以全速运行,而不必担心类型。
    • 它会导致较少数量的运行时异常或与类型相关的错误,因为大多数类型检查是在编译时完成的。
  • “动态”类型语言的优点是:
    • 它们可以帮助实现极快的原型设计,因为开发人员不需要了解类型系统,因此开发人员可以松散地创建变量并运行它,从而实现非常快速的原型设计。
  • 静态和动态类型语言列表
    • 静态:
      • Java
      • C(C 是一种静态类型语言,但与 Java 相比“强”类型较少,因为它允许更多隐式转换)
      • C++
      • C#
    • 动态:
      • PERL
      • PHP
      • Python
      • JavaScript
      • 红宝石
    • 静态地

  • 类型检查是一项重要的安全功能。假设没有类型检查,并且一个方法接受类型为“BankAccount”的对象,该对象有一个名为“ CreditAccount(BankAccountDetails)”,现在在运行时如果没有类型检查,那么我可以传递一个我自己的类的对象,该对象具有相同的方法“creditAccount(BankAccountDetails)”并且它将被执行,考虑到我们正在谈论面向对象语言,因为OOP支持“多态”,这里我们讨论的只是“多态”。因此,基本上,一种没有强大类型检查的面向对象语言(这基本上意味着它支持“多态性”)可能会导致安全问题。

强类型语言与弱类型语言

  • 强类型语言是那些在精度损失的情况下不允许隐式转换的语言。例如,在 Java 中,您可以将“int 转换为 long”,因为不会损失精度,但您不能“隐式”将“long 转换为 int”,因为会损失精度。相反,在弱类型语言中,即使存在精度损失,也允许隐式转换。
  • 我认为动态类型语言也可以是强类型语言,如果“在运行时”它不允许隐式转换,其中存在精度损失。

很好的进一步阅读

Statically v/s dynamically typed languages

  • 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.

Strongly v/s weakly typed languages

  • 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.

Good further readings

情绪操控生活 2024-08-29 21:27:40

在编程中,数据类型是一种分类,它告诉变量将保存什么类型的值,以及可以对这些值执行哪些数学、关系和逻辑运算而不会出错。

在每种编程语言中,为了最大限度地减少出错的机会,都会在程序执行之前或期间进行类型检查。 根据类型检查的时机,编程语言分为两种类型:静态类型动态类型语言。

另外,根据隐式类型转换是否发生,编程语言有两种类型:强类型弱类型语言。

打字-静态vs 动态,强 vs 弱

静态类型:

  • 类型检查在编译时完成

  • 在源代码中,在变量声明时,必须显式指定该变量的数据类型。因为如果在源代码中指定了数据类型,那么在编译时源代码将转换为机器代码并且可以进行类型检查

  • 这里数据类型与变量关联,例如,int计数。并且这种关联是静态的或固定的


  • 如果我们尝试通过分配其他数据类型的值(int count = "Hello ") 进入它,然后我们会得到错误

  • 如果我们尝试通过使用其他数据类型(boolean count)重新声明已声明的变量(int count)来更改数据类型,那么我们也会 得到错误得到错误

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 
  • As type检查和类型错误检测是在编译时完成的,这就是为什么在运行时不需要进一步的类型检查。因此程序变得更加优化,执行速度更快

  • 如果我们想要更严格的代码,那么选择这种类型的语言是更好的选择

  • 示例:Java、C、C++、Go、Swift 等

动态类型:

  • 类型检查在运行时完成

  • 在源代码中,在变量声明时,不需要显式指定该变量的数据类型。因为在运行时进行类型检查时,语言系统根据分配给该变量的值的数据类型来确定变量类型

  • 这里数据类型与分配给变量的值相关联,例如,var foo = 10, 10 是一个数字,所以现在 foo 是数字数据类型。但这种关联是动态的或灵活的

  • 我们可以通过分配其他数据类型的值(foo = " Hi") 进去,没有错误

  • 我们可以通过使用其他数据类型的值重新声明它 (var foo = true) 来轻松更改已声明变量 (var foo = 10) 的数据类型 (var foo = true) >),没有错误

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 等

强类型:

  • 数据类型相关规则和限制严格维护

  • 从一种数据类型到另一种数据类型的转换必须显式完成< /strong>,没有隐式类型转换


# in python, "5" cannot automatically get converted to 5
pybar = "5"

print(10 + pybar)     # error, no `+` operation between `int` and `str` 
  • 可以在编译时或运行时进行类型检查。这意味着强类型语言可以是静态类型的,也可以是动态类型的

  • 示例:Python、Java、Ruby、C# 等

弱类型:

  • 数据类型相关规则和限制松散维护

  • 从一种数据类型到另一种数据类型的转换可以隐式发生

  • 如果我们在两个不匹配的数据类型的值之间执行某些操作,那么这种类型的语言可能不会抛出错误。相反,弱类型语言将应用自己的隐式类型转换规则并返回一些结果

jsbar = "5";

alert(10 + jsbar);  /* "105", no error as javascript implicitly coerces Number 10 
to String "10", so that it can be concatenated with other operand jsbar i.e. "5" */
  • 类型检查可以在编译时或运行时完成。这意味着弱类型语言可以是静态类型的,也可以是动态类型的

  • 示例:JavaScript、C、C++、PHP 等

In Programming, Data Type is a Classification which tells what type of value a variable will hold and what are the mathematical, relational and logical operations can be done on those values without getting error.

In each programming language, to minimize the chance of getting error, type checking is done either before or during program execution. Depending on the Timing of Type Checking, programming languages are 2 types : Statically Typed and Dynamically Typed languages.

Also depending on whether Implicit Type Conversion happens or not, programming languages are 2 types : Strongly Typed and Weakly Typed languages.

typing- static vs dynamic, strong vs weak

Statically Typed :

  • 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 
  • As type checking and type error detection is done at compile time that's why during runtime no further type checking is needed. Thus program becomes more optimized, results in faster execution

  • If we want more rigid code then choosing this type of language is better option

  • Example : Java, C, C++, Go, Swift etc.

Dynamically Typed :

  • 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 
  • As type checking and type error detection is done at runtime, that's why program becomes less optimized, results in slower execution. Although execution of these type of languages can be faster if they implement JIT Compiler

  • If we want to write and execute code easily then this type of language is better option, but here we can get runtime error

  • Example : Python, JavaScript, PHP, Ruby etc.

Strongly Typed :

  • Data type related rules and restrictions are Strictly maintained

  • Conversion from one data type to another data type must be done explicitly, no implicit type conversion

# in python, "5" cannot automatically get converted to 5
pybar = "5"

print(10 + pybar)     # error, no `+` operation between `int` and `str` 
  • type checking may be done at compile time or runtime. It means strongly typed languages can be either statically typed or dynamically typed

  • Example : Python, Java, Ruby, C# etc.

Weakly Typed :

  • Data type related rules and restrictions are Loosely maintained

  • Conversion from one data type to another data type can occur implicitly

  • If we perform some operation between values of two mismatched data types then this type of language may not throw error. Instead weakly typed languages will apply their own rules for implicit type conversion and will return some result

jsbar = "5";

alert(10 + jsbar);  /* "105", no error as javascript implicitly coerces Number 10 
to String "10", so that it can be concatenated with other operand jsbar i.e. "5" */
  • type checking may be done at compile time or runtime. It means weakly typed languages can be either statically typed or dynamically typed

  • Example : JavaScript, C, C++, PHP etc.

物价感观 2024-08-29 21:27:40

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

这里有两个例子:

  • 有人说 Haskell 是强类型的,因为你不被允许进行任何类型转换。

  • 其他人(例如 Dario 的观点)说,一种允许故意从字符串隐式转换为数字的语言是弱类型的,但甚至其他人也称这只是鸭子类型。

这两种说法都强调的不是类型系统的相反极端,而是完全不同的方面。所以我同意拉姆齐先生的观点,不要使用“强”和“弱”这两个术语来区分类型系统。

I think the other colleagues made a good job esp. explaining the difference between static and dynamic typing. But as far as strong and weak typing is concerned, it should be said that there are
different understandings/views.

Here two examples:

  • Some say that Haskell is strongly typed, because you are not allowed to make any type conversions.

  • Others (e.g. Dario's view) say a language that allows to implicitly convert from string to number on purpose is weakly typed, but even others call this just duck typing.

Both statements highlight not the opposite extremes of a type system, but completely different aspects. So I join Mr. Ramsey's view not to use the terms "strong" and "weak" to distinguish between type systems.

我不会写诗 2024-08-29 21:27:40

来自 Addison Wesley,《面向对象的分析和应用程序设计》,第 3 期,第 66 页:

强类型和弱类型以及静态类型和动态类型的概念
完全不同。强类型和弱类型指的是类型
一致性,而静态和动态类型指的是时间
名称与类型绑定。静态类型(也称为静态绑定
或早期绑定)意味着所有变量的类型和
表达式在编译时是固定的;动态类型(也
称为后期绑定)意味着所有变量的类型和
表达式直到运行时才知道。一种语言可能两者兼而有之
强类型和静态类型 (Ada),强类型但支持
动态类型(C++、Java),或无类型但支持动态类型
打字(Smalltalk)。

From Addison Wesley, Object Oriented Analysis and Design with Applications, 3rd, page-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).

冬天的雪花 2024-08-29 21:27:40

静态类型语言通常要求声明变量的类型,然后在编译时检查以减少错误。 “静态类型”中的“静态”一词指的是“静态代码分析”,即在执行代码之前检查代码的过程。尽管静态类型语言可以从表达式或实际参数的右侧推断变量的类型,但实际上大多数静态类型语言都需要显式声明变量类型。

动态类型语言通常不要求变量声明具有类型,并且它们根据计算每个赋值语句的右侧或函数调用的实际参数而计算出的类型来推断变量类型。由于变量在其生命周期内可以被多次赋值,因此它的类型可以随着时间的推移而改变,这就是它被称为“动态类型”的原因。此外,运行时环境需要跟踪每个变量的当前类型,因此类型绑定到值而不是变量声明。这可以被认为是运行时类型信息(RTTI)系统。

静态和动态类型语言的元素可以组合。例如,C# 支持静态和动态类型变量,面向对象语言通常支持类型层次结构的向下转换。静态类型语言通常提供各种绕过类型检查的方法,例如使用强制转换、反射和动态调用。

强类型与弱类型是指语言试图防止由于使用变量而导致错误的连续体,就好像它是一种类型,而实际上它是另一种类型。例如,C 和 Java 都是静态类型语言,但是 Java 使用比 C 更强的类型检查。以下 C 代码很容易编译和运行,并且会在运行时将随机值放入变量 b 中,很可能会导致bug:

char *a = "123";
int b = (int)a;

等效的 Java 代码会产生编译错误,这通常是更好的选择:

String a = "123"
int b = (int)a;

Statically typed languages generally require you to declare the types of variables, which is then checked at compile time to reduce errors. The word "static" in "statically typed" refers to "static code analysis", which is the process of examining the code prior to executing it. Although it is possible for a statically typed language to infer the type of the variable from the right hand side of an expression or actual parameters, in practice most statically typed languages require variable types to be explicitly declared.

Dynamically typed languages generally do not require variable declarations to have types, and they infer variable types based on the type calculated as a result of evaluating the right hand side of every assignment statement or the actual parameters to a function call. Since the variable can be given multiple assignments over its lifetime, its type can change over time and this is why it is called "dynamically typed". Also, the runtime environment needs to keep track of the current type for each variable, so the type is bound to the value rather than with the variable declaration. This can be considered a runtime type information (RTTI) system.

Elements of statically and dynamically typed languages can be combined. For example, C# supports both statically and dynamically typed variables, and object oriented languages generally support down-casting the type hierarchy. Statically typed languages usually provide various ways to bypass type checking, for example by using casting, reflection and dynamic invocation.

Strong vs. Weak Typing refers to a continuum of how much the language tries to prevent bugs due to using a variable as if it were one type when it is in fact another type. For example both C and Java are statically typed languages, however Java uses much stronger type checking than does C. The following C code is happy to compile and run, and will put a random value into the variable b at runtime, most likely causing a bug:

char *a = "123";
int b = (int)a;

The equivalent Java code will produce a compile error, which is generally preferable:

String a = "123"
int b = (int)a;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文