Jump to content

Talk:Comparison of Java and C++

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by CmdrRickHunter (talk | contribs) at 06:06, 2 August 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

There are some on-line articles comparing Java with C++. Some of them emphasize C++'s advantages, while others highlight Java's. Who's that guy who write "Thinking in Java"? He had a pretty comprehensive one, although it's dated now. User:Ed Poor

Maintaining a NPOV

A comparison of Java to C++ should list differences. Calling those differences "advantages" or "disadvantages" of one language or the other is in most cases controversial and shows bias. Additionally, it's often impossible to argue in favor or against a specific language feature without considering the broader perspective and intent of the languages.

In the interest of maintaining a NPOV, I have reworded and moved all items from the "advantages"/"disadvantages" lists to the "differences" list. In addition, I've added a paragraph that describes the cause of many differences: differing design aims.

I urge future editors to not show bias.

-- Eelis 01:53, 2005 May 23 (UTC)


Someone deleted information about 64-bit numbers (from IP 213.94.248.38 on July 8), claiming it to be inaccurate. I have restored this information, and added a link to the 64-bit page to allow editors to vouche its authenticity, along with clearer wording. CmdrRickHunter 06:06, 2 August 2006 (UTC)[reply]


A previous edit indicated that there was dispute over whether or not C++ is better for "DSP or arithmatic heavy" operations.

I would like to add text clarifying it, but I do not feel that I have fully researched the topic. Here are my points:

  • For sequential arithmatic, Java and C++ are on the same level. However, Java cannot support vector processing because its very processor dependant. C++ allows one to craft code designed for specific processors, opening up the use of SSE or AltiVec.
  • A search for Java DSP or Java vector arithmatic yields no hits for while doing similar searches for C++ yield libraries designed for native interfacing with the vector processors.

Based on this, I feel the statment should be added back into the article, with clarification. However, since I am new to wikipedia, I'd at least like to see if anyone has feedback on the idea.CmdrRickHunter 21:32, 30 June 2006 (UTC)[reply]


I'm going to remove the comment that this article should be merged because I don't agree with the reasons, and the fact that editing has remained active for over 5 months and about 40 edits leads me to believe there are plenty of others to support my position. If this should be merged, I think there are enough people who have worked on this article that there should be some discussion here first. I don't want to see an overzealous and possibly inexperienced editor coming along, see the merge task as a community decision waiting to be implemented, and do the merge without discussion. CyborgTosser (Only half the battle) 20:22, 25 Feb 2005 (UTC)

For reference, here is the original merge request as posted by Netoholic:

This article or section should be merged with the Java programming language & the C++ programming language.

CyborgTosser (Only half the battle) 20:27, 25 Feb 2005 (UTC)

My main point is that there are infinite combinations of comparison articles possible (just the top 10 languages alone would lead to 45 such articles). A much better approach would be to put the "Advantages" and "Disavantage" sections within each language article itself. That way, you can reduce the redundancy where a language has dis/advantages over multiple other ones. -- Netoholic @ 22:12, 2005 Feb 25 (UTC)
There are many possible combinations for articles, but probably only about a dozen that would make interesting articles. In particular, when one language is strongly influenced by another (this article) or if the languages represent a split in design philosophy within a given paradigm (such as OCaml vs. Lisp), there is a possibility for an interesting article. An advantages/disadvantages section in each article would be a good idea, but as long as the number of comparison articles we have doesn't grow without bounds, I don't think there will be a lot of redundancy. Look, for example, at this article and the comparison of Java to C#. What gives Java an advantage/disadvantage compared to C++ is not the same as compared to C#. But I will take a "wait and see" attitude on this. If I am wrong and there is a ton of redundancy as more of these articles pop up, then I'm in support of consolidation. CyborgTosser (Only half the battle) 23:28, 25 Feb 2005 (UTC)

resource

This article equates resource with memory, but a resource might also be a lock that is acquired and lots of other things too.--MarSch 15:05, 27 October 2005 (UTC)[reply]

contributions by 68.68.100.185

The biggest consequence of this is that you can't have const parameter types in Java. - this is at least incredibly misleading if not incorrect. to properly explain this would require more space than this article should dedicate to the subject. someone interested in knowing more should research it further.

Destructors are essentially what enable the RAII idiom, as deallocation is done in the destructor. This means that the RAII idiom is impossible to implement properly in Java... - besides being poorly written/explained, i think this is too much depth for the article, but i think drawing a little more distinction between destructors and finalizers is appropriate.

i've updated accordingly. critique, rebut, modify at will ;) --pfunk42 14:54, 26 December 2005 (UTC)[reply]

Edits by 84.61.26.158 on January 10, 2006

There is a pro-C++ (or perhaps anti-Java) POV in many of these contributions. I've lightly editted some of them to attempt NPOV. I admit to having my own bias, which I am trying not to include in the article. There are some specific points I'll address here for discussion before making additional changes to the article. Additions from 84.61.26.158 in the excepts from the article below are shown in bold italics.

  • C++ features callbacks, which must be simulated in Java using trampolines.
I have a couple issues with this. First, callbacks are not a feature of C++, but rather a use of function pointers (or functors). So a callback is not a feature of C++ (although function pointer is a feature that can be compared). Second, the mechanism in Java is to use an object for a callback, or more pointedly, to define the callback method using an interface and pass an object that implements the interface in place of the C++ function pointer. This mechanism does not match the definition of a trampoline. -- Doug Bell 23:12, 10 January 2006 (UTC)[reply]
Removing it, but a good statement regarding function pointers should be put in. --pfunk42 19:10, 11 January 2006 (UTC)[reply]
  • C++ supports goto statements; Java does not, but its labelled breaks provide some goto-like functionality. In fact, Java enforces structured control flow, with the goal of code being easier to understand. That can make code distintly less readable.
I corrected the spelling, but left the statement. My problem with this is that while there can be a case made for limited circumstances where a goto can make code more readable (and most of these cases are obviated by the use of exceptions to handle errors and exception execution paths), there are many more cases where the use of goto makes code less readable. That makes this statement an unbalanced POV. -- Doug Bell 23:12, 10 January 2006 (UTC)[reply]
That can make code distinctly less readable. I have no idea what "that" is supposed to refer to, so this statement is gone. I see no POV problem with the way it was because with the goal of leaves the reader to draw his/her own conclusion as to whether enforcing structured control flow makes code easier to understand. --pfunk42 19:10, 11 January 2006 (UTC)[reply]
  • The encoding of string and character literals in C++ is as the programmer sees fit.
This used to say "platform dependant". I wasn't aware that it was a feature of C++ to allow the programmer to determine how the compiler encodes string and character literals. Perhaps this is a feature of some specific C++ compiler? -- Doug Bell 23:12, 10 January 2006 (UTC)[reply]
I'm reverting it. 84.61.26.158 is wrong. --pfunk42 19:10, 11 January 2006 (UTC)[reply]
  • In Java, all code is implicitly multithreaded, though most programmers are unaware of that fact. Deadlocks can thus easily happen.
I have a few issues with this statement.
  • Deadlocks do not easily happen in Java because of the inherent multi-threaded nature of Java programs. This is because unless a Java program is operating in a multi-threaded context, such as an Applet or applications with a GUI, all of the main application code and objects run in, and are accessed from, a single thread. There is no opportunity for contention or deadlock.
  • Both C++ and Java programs that run in a multi-threaded context can suffer from problems inherent in multi-threaded programs (deadlocks and race conditions). This includes any applications that support call backs or events, to name a couple. This means that the issue of deadlock is not a Java phenomenon, but rather a multi-threaded programming phenomenon relavent to both Java and C++.
  • The statement that most programmers are unaware of when they are programming in a multi-threaded context is unsubstantiated speculation.
I'm assuming that the underlying issue here is the Java synchronized mechanism, but that's not readily apparent from the statement. Deadlock could also occur in a multi-threaded C++ program that used mutex locks to guard against contention, except that in the case of the C++ program, the release of the mutex lock is not enforced by the programming language. So it can be argued that deadlock is more likely in the multi-threaded C++ program than in the comparable Java program. -- Doug Bell 23:12, 10 January 2006 (UTC)[reply]
I'm removing it. If you don't declare multiple threads in your code, deadlock is impossible. Wait, the Swing thread pops up implicitly, but that doesn't fall under "happening easily in all code". --pfunk42 19:10, 11 January 2006 (UTC)[reply]
Actually, System.runFinalizersOnExit has been deprecated for some time. So the statement which might never happen is correct. There is no way to force finalization to occur. Even calling System.runFinalization will not guarantee execution of the finalizers. I readded the statement and expanded the comparison of destructors and finalizers.-- Doug Bell talk/contrib 21:13, 11 January 2006 (UTC)[reply]

references for the design goals of C++ (and Java)

The article contains this table:

The different goals in the development of C++ and Java resulted in different principles and design tradeoffs between the languages.

C++ Java
execution efficiency developer productivity
trusts the programmer protects the programmer
arbitrary memory access possible memory access only through objects
concise expression explicit operation
can arbitrarily override types type safety
procedural and object-oriented object-oriented
redefine operators meaning of operators immutable
feature rich easy to use

which implies that for example execution efficiency and developer productivity are opposites. It is my understanding that C++ aims for both of these (and supports developer productivity with generic programming, unlike Java), while this table seems to say that using java allows for greater developer productivity. All other entries also suffer from POV or are simply false. I'd like some references for this table, for example from The Design and Evolution of C++. The fact Java doesn't support operator-overloading or templates, doesn't make it easier to use. What it does mean is that C++ has a far more powerful way of expressing, resulting in generic algorithms. Java doesn't protect the programmer, it forces the programmer not to do certain things. C++ protects the programmer, by providing type safety, but also providing casts. I think there is a lot of pro-Java POV in this article. Even the title (Comparison of Java to C++) is POV. MarSch 13:21, 5 March 2006 (UTC)[reply]

  • if your commentary isn't a totally biased Java flame, i don't know what is. POV! POV! POV!!! (i can say it too). . . . more seriously, i think you're taking the table to mean a lot more than intended. the table outlines rough design goals. it's not meant to be conclusionary about the efficacy or merits of those goals, or how elegantly the languages fulfill those goals. . . . but also, some of what you say is just wrong. :) --pfunk42 14:58, 5 March 2006 (UTC)[reply]
  • While, as pfunk42 points out, there are many points where MarSch is just wrong, I think s/he makes a couple good points. First, a better name for the article would be Comparison of Java and C++. Second, there should be references for the languages respective design goals. However, these points are completely wrong:
OK, I renamed the page as above. – Doug Bell talkcontrib 05:00, 6 March 2006 (UTC)[reply]
    • implies that for example execution efficiency and developer productivity are opposites
      Nothing of the sort is implied. What is stated is that each language had principles that established priorities that guided the design of the language.
    • supports developer productivity with generic programming, unlike Java
      Uh, right there in the article it talks about comparing generics and templates and even references a sibling article Comparison of generics and templates that compares the two features in depth.
    • All other entries also suffer from POV or are simply false.
      This statement clearly suffers from both POV and simply being false.
    • The fact Java doesn't support operator-overloading or templates, doesn't make it easier to use.
      A point that can be argued to death without ever reaching a decisive conclusion because "easier" is subjective and not uniquely quantifiable...however, the points in the table are, as pfunk42 points out, discussing goals, not outcome.
The rest of the points raised by MarSch are POV. This article has been edited by both Java, C++, and Java + C++ users and while it can be better, it is not a one-sided myopic view of the subject as MarSch seems to suggest. – Doug Bell talkcontrib 18:23, 5 March 2006 (UTC)[reply]

Cleanup tags

I've added three cleanup tags to this article.

  • Tone: This article consists mainly of bulleted lists of features, rather than a more encyclopedic discussion of fundamental issues.
  • Not verified: The article does not cite sources sufficiently, especially in the "Design aims" section; it makes a number of unverified (and implementation-dependent) claims about speed and size, as well as a few about ease of programming and/or maintenance.
  • Importance: What information does this article provide that is not already covered by Java programming language and C++?

donhalcon 05:47, 6 March 2006 (UTC)[reply]

Syntax power!

"C++ has more powerful syntax than Java", says the article. Right. Powerful. Precisely what, may I ask, does "powerful" mean in the context of syntax?

The whole thing seems to be like that. This article doesn't need cleanup, it needs deleting completely and rewriting from scratch, preferably by someone who doesn't prefer either language and is therefore capable of viewing them objectively. — Haeleth Talk 23:15, 16 May 2006 (UTC)[reply]

"power" in the sense of doing more with less. --pfunk42 16:56, 3 June 2006 (UTC)[reply]
in which case, "expressive" or "compact" are more specific adjectives that might be worth considering instead. JulesH 10:43, 17 July 2006 (UTC)[reply]

Java-centric

A partisan article like the one comparing Java and C++ has no place in Wikipedia. Its Java drum-beating is embarrassing to anyone who has dealt seriously with the range of computer languages. This piece is error-ridden and should be rewritten from the ground up.

A more general issue is, what is the point of an encylopaedia article that takes two particulars from a larger set and compares them? That is -- C++ and Java are just two of several commonly used object- (and function-) oriented languages. Where are the others? Python, LISP, Delphi, Smalltalk... these are languages that have significant industrial usage around the world. They all have thousands of advocates who can intelligently defend their particular advantages. (For example, space and atomic energy enterprises more commonly use LISP dialects than C++ or Java for their generalized applications.)

I believe that an article comparing all the major object- and function-oriented languages would be useful in this encyclopaedia. It would be difficult to write in a fair manner, but could be quite useful -- even influential -- if it were done well.

Some inaccuracies

Some things I believe are inaccurate, but which I'm not updating because I don't have sources for most of this.

Still it's possible to have memory leaks in java, via cross-referencing, on most JVM implementations. Two unused object (not referenced anymore by the main application) could reference themselves so they are still marked as used and not garbage-collected.

As I understand it, most if not all Java implementations use mark and sweep garbage collection or some similar method to collect cyclical references. Certainly all the ones I've worked on do not suffer this problem. It is possible to have memory leaks, but only by making objects that will never be used again theoretically reachable if you followed the right chain of references.

In Java, assembly code can only be accessed as libraries, through the Java Native Interface. However, there is significant overhead for each call.

This depends on the implementation of Java you are using. There is nothing in the Java Language or Virtual Machine specifications that requires the use of JNI for any native code calls. See, e.g., gcj's "CNI" implementation, which allows a native method to be implemented in C++ and directly linked to the class that uses it[1]. In this case, the overhead is significantly lower, and is in many cases as efficient as a C++ method call.

The same objection also applies to the statement "Direct access from Java to native operating system and hardware functions requires the use of the Java Native Interface."

Both Java and C++ distinguish between native types (these are also known as "fundamental" or "built-in" types) and user-defined types (these are also known as "compound" types). However, in C++ this distinction is far less extensive.

This sentence is meaningless. It should be expanded with specific examples of why the distinction is less extensive in C++ (e.g. ability to construct a reference to fundamental types in C++ but not in Java).

Due to the lack of constraints in the use of some C++ language features (e.g. unchecked array access, raw pointers), programming errors can lead to low-level buffer overflows, page faults, and segmentation faults.

Java can cause page faults too, you know. Changing "page faults and segmentation faults" to "memory access violations" might make sense.

In C++, if you have an array, or a pointer to an array, or a reference to an array, you can determine the array's size using the "sizeof" operator; however, if you only have a pointer to the first element of an array, its size cannot be determined.

I think this might better be phrased as "In C++ the size of an array may only be determined (using the "sizeof" operator) statically at compile time; it is impossible to determine the size of an array from a pointer to its first element." The text in the article appears to suggest that some construct like the following example would work:

 void myFunction (int (&anArray)[])
 {
     cout << sizeof(anArray) << endl;
 }

It won't (you'll get a compiler error in the declaration of the function, as the size of a referenced array must be known statically at compile time).

(All of the above JulesH 11:06, 17 July 2006 (UTC))[reply]