谁能解释一下 C C++ Java关于动态或静态语言
任何人都可以解释关于动态类型或静态类型语言的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还有哪些意见?毫无疑问,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
这是一个频谱。 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.在赫洛夫达尔的回答之后,我将再次引用本杰明·皮尔斯的话,对这个问题提出积极的看法。我参考并扩展了他的《类型和编程语言》的第一章。
Java 是一种安全语言(即可以防止运行时类型错误),主要进行静态类型检查。然而,由于继承(更准确地说,子类型化),变量的类型可以比指向值的类型更通用。
此外,该语言还允许验证对象是否具有某种类型,并在运行时向下转换对象 - 在此类操作期间,会在运行时检查类型。因此,每个对象都有一个指向其类型的运行时表示的指针。
C 是一种具有静态类型检查的不安全语言。类型在运行时不存在。
C++ 仍然是一种具有静态类型检查的不安全语言,但它还具有对满足特定条件的类进行有限的运行时类型识别的功能,即具有一些虚拟方法(如 Java 中的所有对象)。
编辑:“静态类型”是一个不是明确定义的概念。为了表明这一点,我将(松散地)定义三个属性,其中一个可能与“静态类型”相关。
如果你说一种语言具有我上面提到的所有三个属性,那么它就是“静态类型”,那么 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".
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.
关于静态/强类型的不同意见,我碰巧偶然发现了这个旧的slashdot评论 读完这个问题后:
Regarding different opinions on static/strong typing I happened to stumble over this old slashdot comment just after reading this question: