can anybody explain C C++ Java regarding dynamic or static language
Asked Answered
S

4

5

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!

Stephanistephania answered 6/3, 2011 at 0:27 Comment(1)
You probably mean statically typed and dynamically typed.Abisha
C
6

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

Celeski answered 6/3, 2011 at 0:41 Comment(0)
P
1

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.

Popelka answered 6/3, 2011 at 0:37 Comment(8)
If you consider template, c++ has duck-typing, albeit at compile time.Ciprian
@Ciprian Templates are completely, utterly 100% statically typed. If generics were the same as duck typing then there would be no need for the latter term. Also, dynamic dispatch as mentioned above is not dynamic typing -- the subject of the question -- and doesn't have anything to do with dynamic typing.Celeski
@Ciprian is right, though one must refine the terminology. In C++ you cannot predict whether template expansion will succeed before executing it; this is the essential characteristic of a dynamically typed language, even if it executes at a different stage. Contrast with templates with concepts, where you can define a "concept" of InputIterator (the equivalent of an interface) and can check if a template instantiation is valid or not before expanding it.Excommunication
@Excommunication No, you're completely wrong. Template expansion isn't "executed", it is compiled ... static vs. dynamic is entirely a matter of compile-time vs. run-time. We now have hefty Turing machine compile-time evaluation in C++ ... but it's still compile-time, so still static ... catching things before producing a runnable object is what static typing is all about.Celeski
@JimBalter: In the academic literature, C++ templates are a form of macro (see e.g. brics.dk/bigwig/publications/macro.pdf), which is not fully statically typed. In such cases, there's more than compile-time and runtime — feel free to familiarize yourself with multi-stage programming cs.rice.edu/~taha/MSP. I refer to the "runtime" of templates inside the compiler — which is the compile-time of the C++ program itself.Excommunication
Template are metaprograms, and template expansion is the execution of these metaprograms — but their runtime aligns with the compile-time of the manipulated program. Their "compile-time" is when the templates itself is parsed and typechecked. Many (but not all) bugs in templates will only be caught when you apply the template, because of the lack of concepts. I see you served on the C standardization committee. I'm instead a young researcher in programming languages, so my terminology often disagrees with the C/C++ one because many concepts occur across languages.Excommunication
Still completely wrong. Execution time is when the program is run. That distinction matters, as any professional developer knows. The fact that templates aren't elaborated at the point of definition makes for inscrutable error messages at the point of use, which is what concepts are the cure for, but that's still a compile-time occurrence.Celeski
That compile-time occurrences can be divided into point-of-definition and point-of-use has no bearing on the important compile-time (static) vs. execution-time (dynamic) distinction. And your ageism ad hominem is ridiculous. Age has no bearing on familiarity with new technology, and I am familiar with it.Celeski
E
1

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.

Excommunication answered 26/3, 2011 at 23:6 Comment(2)
You have the direction backwards. The fact that a reference variable's type can be more generic than what it references means that no runtime check is needed. Downcasts convert a generic type to a more specific one and are a code smell.Celeski
@JimBalter: I clarified my answer, though in a different way.Excommunication
S
0

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.

Sawdust answered 6/3, 2011 at 1:51 Comment(6)
This isn't any sort of answer to the question asked, which is about whether C, C++, and Java are statically typed ... which is not at all a useless term.Celeski
Benjamin Pierce did include statically typed in his quote about "the usage of these terms is so various as to render them almost useless.", so I do not think my answer was offtopic. When does a language stop being statically typed and start being dynamically typed? When it has some dynamically typed features? When it has several dynamically typed features? Most dynamically typed features? Only dynamically typed features? I agree that C, C++ and Java are statically typed, but there are still room for different meanings, and that was specifically given as the reason for the question being asked.Sawdust
An "answer" that quotes someone as saying that they tried to figure out what terms mean but couldn't isn't an answer to the question, and does nothing to change the fact that you mixed up static typing -- the subject of the question -- and strong typing. "there are still room for different meanings, and that was specifically given as the reason for the question being asked." -- no it wasn't.Celeski
@JimBalter: this "answer" quotes very respected programming language researchers, which explain that "statically typed" is used in so many different ways that it is useless. The OP stated there are different opinion on these languages; the reason is that the terms are ill-defined, as explained here. In other words, this answer explains why there are so many different opinions on these topics.Excommunication
@JimBalter: Pierce explains that also "statically typed" is an ill-defined expression.Excommunication
@Excommunication He didn't "explain" anything, and the claim is false. Also your claim about "very respected programming language researchers" is false, as the first quote is about strongly typed. And the OP is self-admittingly confused and claimed "other opinions" that don't exist ... no one anywhere asserts that C, C++, and Java are not statically typed languages. Now, these comment threads are not for extended discussion, especially with people who insist on falsehoods, so this is the last I'll say here.Celeski

© 2022 - 2024 — McMap. All rights reserved.