开发者

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

开发者 https://www.devze.com 2023-02-15 05:34 出处:网络
can anybody explain C C++ Java regarding dynamic typed or static typed language. I read somewhere that C C++ and Java are all static langu开发者_高级运维age. But I recall there are other opinions ab

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

I read somewhere that C C++ and Java are all static langu开发者_高级运维age. But I recall there are other opinions about these. Very confused.

Thanks!


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消