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!
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!
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.
template
, c++ has duck-typing, albeit at compile time. –
Ciprian 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.
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.
© 2022 - 2024 — McMap. All rights reserved.