谁能解释一下 C C++ Java关于动态或静态语言

发布于 2024-10-21 00:49:26 字数 104 浏览 1 评论 0原文

任何人都可以解释关于动态类型或静态类型语言的 C C++ Java 吗?

我在某处读到C C++和Java都是静态语言。但我记得对此还有其他看法。很困惑。

谢谢!

can anybody explain C C++ Java regarding dynamic typed or static typed language.

I read somewhere that C C++ and Java are all static language. But I recall there are other opinions about these. Very confused.

Thanks!

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

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

发布评论

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

评论(4

︶ ̄淡然 2024-10-28 00:49:26

还有哪些意见?毫无疑问,C、C++ 和 Java 都是静态类型语言,其中 C++ 和 Java 具有一些动态类型功能。还有另一个问题:强类型与弱类型,这主要与隐式转换和重载有关。网络上有许多关于这些问题的深入讨论;您可能想从 http://en.wikipedia.org/wiki/Type_system 开始

What other opinions? There is no question that C, C++, and Java are all statically typed languages, with C++ and Java having some dynamically typed features. There's another issue: strongly vs. weakly typed, which pertains primarily to implicit conversions and overloading. There are numerous in-depth discussions of these issues available on the web; you might want to start with http://en.wikipedia.org/wiki/Type_system

瑾兮 2024-10-28 00:49:26

这是一个频谱。 C 没有任何动态类型功能,尽管它允许您使用 void * 和强制转换来自己进行一些欺骗。 C++ 和 Java 对类方法进行动态分派,因此在 C++ 和 Java 中,有时您直到运行时才知道对象上实际调用了哪个方法。 Java 包含一个反射 API,实际上可以让您在运行时检查和修改类型,因此它比 C++ 更加动态。还有像 Python 和 Ruby 这样的语言,它们几乎是完全动态的 - 在编译时几乎没有检查任何内容,并且具有诸如“鸭子类型”之类的功能,只要它支持您关心的操作。

It's a spectrum. C doesn't have any dynamic typing features, although it allows you to use void * and casts to do some trickery yourself. C++ and Java have dynamic dispatch on class methods, so there are cases in C++ and Java where you don't know which method is actually being called on an object until run time. Java includes a reflection API that actually lets you inspect and modify types at run time, so it's a bit more dynamic than C++. Then there are languages like Python and Ruby which are pretty much fully dynamic - almost nothing is checked at compile time, and you have features like "duck typing" where you don't care too much about the actual type as long as it supports the operation you care about.

囚我心虐我身 2024-10-28 00:49:26

在赫洛夫达尔的回答之后,我将再次引用本杰明·皮尔斯的话,对这个问题提出积极的看法。我参考并扩展了他的《类型和编程语言》的第一章。

Java 是一种安全语言(即可以防止运行时类型错误),主要进行静态类型检查。然而,由于继承(更准确地说,子类型化),变量的类型可以比指向值的类型更通用。
此外,该语言还允许验证对象是否具有某种类型,并在运行时向下转换对象 - 在此类操作期间,会在运行时检查类型。因此,每个对象都有一个指向其类型的运行时表示的指针。

C 是一种具有静态类型检查的不安全语言。类型在运行时不存在。
C++ 仍然是一种具有静态类型检查的不安全语言,但它还具有对满足特定条件的类进行有限的运行时类型识别的功能,即具有一些虚拟方法(如 Java 中的所有对象)。

编辑:“静态类型”是一个不是明确定义的概念。为了表明这一点,我将(松散地)定义三个属性,其中一个可能与“静态类型”相关。

  1. 在执行程序之前,会对其进行类型检查:Java、C 和 C++ 都满足此标准。
  2. 如果程序类型检查,我们可以保证在运行时它不会出现某一类错误:C 和 C++ 未通过此标准,Java 通过它(尽管这只是可能的,因为失败的强制转换被明确排除在此类错误之外) )。
  3. 运行时不存在语言定义的类型表示。此属性对于 Java 和 C++ 均无效,并且是 C 和 Pascal 等语言的特征。

如果你说一种语言具有我上面提到的所有三个属性,那么它就是“静态类型”,那么 Java、C 和 C++ 都不是静态类型的。

After hlovdal's answer, I will quote Benjamin Pierce again, with something positive on the question. I reference and expand from Chapter 1 of his "Types and Programming Languages".

Java is a safe language (i.e. runtime type errors are prevented) with mostly static type-checking. However, due to inheritance (more precisely, subtyping), the type of a variable can be more generic than the type of the pointed value.
Moreover, the language also allows to verify if an object has a certain type, and to downcast objects at runtime - during such operations, types are checked at runtime. Therefore, each object has a pointer to a runtime representation of its type.

C is a are unsafe languages with static type-checking. Types do not exist at runtime.
C++ is still an unsafe language with static type-checking, but it also features limited run-time type identification for classes satisfying certain condition - i.e. having some virtual method (like all objects in Java).

Edit: "static typing" is a not a well-defined concept. To show that, I'll (loosely) define three properties which one might relate to "static typing".

  1. Before executing the program, it is type checked: both Java, C and C++ satisfy this criterion.
  2. If the program type checks, we can guarantee it that at runtime it will not have errors of a certain class: C and C++ fail this criterion, Java passes it (although this is only possible because failed casts are specifically excluded from this class of errors).
  3. No language-defined type representation exists at runtime. This property fails for both Java and C++, and characterizes languages like C and Pascal.

If you say that a language is "statically typed" if it has all three properties I mentioned above, then neither Java, C nor C++ is statically typed.

芯好空 2024-10-28 00:49:26

关于静态/强类型的不同意见,我碰巧偶然发现了这个旧的slashdot评论 读完这个问题后:

我最喜欢的“强”定义
打字”来自Shriram
Krishnamurthi 的 编程语言:
应用与解释
(第205页):

那么什么是“强类型”?据我们所知,这是毫无意义的
短语,人们经常用它来表达
无意义的时尚。

本杰明·皮尔斯(《类型与类型》一书的作者)
编程语言)写了一些东西
类似(参见 Mark Jason Dominus
引用皮尔斯关于“__打字”
的内容:

大约一年前,我花了几周时间试图整理出
“强类型”的术语,
“静态类型”、“安全”等,以及
发现这非常困难。作为你的
消息指出,这些的用法
术语如此多样以至于使它们
几乎没用。

Regarding different opinions on static/strong typing I happened to stumble over this old slashdot comment just after reading this question:

My favorite definition of "strong
typing" comes from Shriram
Krishnamurthi's Programming Languages:
Application and Interpretation
(p. 205):

So what is "strong typing"? As best as we can tell, this is a meaningless
phrase, and people often use it in a
nonsensical fashion.

Benjamin Pierce (author of Types and
Programming Languages) wrote something
similar (see Mark Jason Dominus
quoting Pierce on "__ typing"
):

I spent a few weeks, about a year ago, trying to sort out the
terminology of "strongly typed,"
"statically typed," "safe," etc., and
found it amazingly difficult. As your
message points out, the usage of these
terms is so various as to render them
almost useless.

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