Q4 ppl/w both C++ and Java experience
Q: This question is for people with both C++ and Java experience. Do you believe there are features of Java that C++ could also have, and would benefit from? My opinion: Typically when Java is praised in the presence of C++ programmers the person extolling any virtue of Java is ridiculed and insinuations about his or her intelligence and abilities are leveled against the person. I believe I am now in a position to speak with some authority regarding the two languages. I may not be one of the leading experts in either language, but I can program effectively in both of them. I firmly believe Java has certain features that make it easier to use, and faster to program in. Among those is a superior mechanism for locating and integrating libraries. In Java the `#include' and `using' declaration (or directive - but I don't do that) are united in the `import' statement. The 'interface' provided by a 'header' file and the 'object' code provided by *.o, *.a, *.la, etc., are combined into a single *.class file. The file is easily located by leveraging the most ubiquitous data storage and retrieval mechanisms in computing; the file system. In the C++ Standard ISO/IEC 14882:2003 there is no formal concept of a header file. All use of header files is by convention, and is based on the ancient artifact inherited from C called the C preprocessor. I know of no fundamental reason why C++ cannot use a mechanism similar to that of Java (and C#, BTW) for locating and importing resources. That one facility would probably increase my productivity to twice what it currently is, simply by freeing me of the task of maintaining mapping identifiers to external resources which often have no direct naming similarity, much less a deterministic relationsip between identifier name and file name. -- Regards, Steven
Without including the quotes, let me answer a few things. First, Java is an entirely new language where C++ is specifically a derivative of C, that includes the power of C. On the same page, C++ also inherits some of the ugly features of C. There are a few features of C++ that are different from C, but for the most part, most C programs will compile and run correctly when built using a C++ compiler. (There are some exceptions). One must also consider that C++ is a compiler<->linker model where the compiler builds a relocatable binary module. The C++ standard does not define how the linker works, but does describe the linkages. Java, on the other hand is designed as an interpretive model where the compiler generates well defined intermediate code (eg. the byte codes) that may be interpreted on every platform that supports the JVM (or JIT). There are also features that were designed into Java that are not part of the C++ language specification such as: Threads. I consider this a plus for Java since the programmer does not need to consider different threading models. Graphics. This is a debatable issue. Some other things I personally prefer in Java over C++: primitive type standards, such as integer sizes. These are all defined by the Java specification so that you are guaranteed an int is 32 bits, a long is 64 bits. In C and C++, an int may be as small as 16 bits and as large as a long (although most 64 bit implementations use a 32 bit int). Longs on a 32 bit system are normally 32 bit and 64 bits on a 64 bit system, and could be 128 bits on a 128 bit system. Character data types in Java are full unicode where they are simply a very short int (eg. 8 bits) in C and C++. Another debatable issues is inheritance. Java uses a single inheritance model where C++ uses multiple inheritance. C++ also supports templates. Templates are very powerful, and allow C++ to use a single container model to contain just about anything. But, there are ways to do similar types of things in Java. Java import vs. C++ preprocessor These are difficult to compare because they do different things. The import statement in Java not only serves a similar purpose to the C++ #include, but also it serves as the linker specification as to which libraries are to be used. The #include files essentially provide feature codes, function prototypes, constants, declarations of variables defined elsewhere, and template and class definitions. But, the preprocessor also contains conditional compilation features. While the portability of Java may mitigate this, the conditional compilation in C++ allows for an implementation to comply with different standards and different platform dependent features. In a complex product, I generally use this to add debugging code that I do not want in production code. Performance: In most cases, a C++ application will significantly outperform a Java application. However, some C++ features make optimization very difficult where a good Java compiler with a good JIT can cause a Java application to perform well. There are many IDEs that make both C++ and Java reasonably easy to use in a graphical environment, simplifying the code.
On Wednesday 15 September 2004 11:05, Jerry Feldman wrote:
Without including the quotes, let me answer a few things. First, Java is an entirely new language where C++ is specifically a derivative of C, that includes the power of C. On the same page, C++ also inherits some of the ugly features of C.
I can't speak with unquestionable authority on this, but I believe you might find Java has it's own hidden legacies. Namely Small Talk and Lisp (spelled Emacs).
Threads. I consider this a plus for Java since the programmer does not need to consider different threading models.
Until very recently, I was of the opinion that C++ was better off without standardizing threading. That allows for other threading 'standards', models, or whatever you want to call them to be created to suite specific needs, or simply to allow for more innovation. Sure with Java you pretty much know what you are dealing with in all cases. But that doesn't allow for much flexibility. The reason I say 'until recently' is because I found this: http://people.redhat.com/drepper/tls.pdf
Graphics. This is a debatable issue.
Some other things I personally prefer in Java over C++: primitive type standards, such as integer sizes. These are all defined by the Java specification so that you are guaranteed an int is 32 bits, a long is 64 bits. In C and C++, an int may be as small as 16 bits and as large as a long (although most 64 bit implementations use a 32 bit int). Longs on a 32 bit system are normally 32 bit and 64 bits on a 64 bit system, and could be 128 bits on a 128 bit system.
My understanding is that the latest C standard actually provides similar definitions. I brought this up on comp.std.c++, but can't say there were any conclusive commitments from anybody.
Character data types in Java are full unicode where they are simply a very short int (eg. 8 bits) in C and C++.
There is the wchar_t, and there are some pretty comprehensive libraries out there for C++. I've looked that this in the past: http://oss.software.ibm.com/icu/
Another debatable issues is inheritance. Java uses a single inheritance model where C++ uses multiple inheritance.
For the most part, I believe C++ has the better model in this respect. Sure there's the evil diamond, but I haven't encountered it that much. There are some costs to using pure virtual classes as interfaces which might be mitigated. I really haven't been able to evaluate this, but I do find it interesting: http://www.heron-centric.com/2004/08/c-with-interfaces-article-addendum.html
C++ also supports templates. Templates are very powerful, and allow C++ to use a single container model to contain just about anything. But, there are ways to do similar types of things in Java.
Yes. Java 1.5-beta has something called generics, but I can't say much about them other than they are advertised as being like C++ templates. Templates are significantly different from the Java brown-paper-container model. C++ templates are 'compiled away', whereas Java containers, as I understand it, are pretty much a runtime entity that doesn't lend itself to the same kinds of optimization that templates provide. I'll grant that Java's containers were much easier for me to learn. I always had a level of dislike for the requirement to pack and unpack things with pervasive castings, which templates don't require. While the portability of Java may I find some uses of the CPP utterly distasteful. I sincerely believe that many of the common uses of macros make it very difficult to create the same level of tools (IDEs) you can find with Java, and the notion of #including multiple levels of source files is simply offensive.
mitigate this, the conditional compilation in C++ allows for an implementation to comply with different standards and different platform dependent features. In a complex product, I generally use this to add debugging code that I do not want in production code.
I've noticed that C/C++ programmers moving to Java tend to take that kind of approach until they discover that exceptions work better in most cases when handling error trapping in Java.
Performance: In most cases, a C++ application will significantly outperform a Java application. However, some C++ features make optimization very difficult where a good Java compiler with a good JIT can cause a Java application to perform well.
From what I understand, the door has only been opened on what can be done with C++ regarding optimization in some situations. These mostly have to do with sophisticated mathematics.
There are many IDEs that make both C++ and Java reasonably easy to use in a graphical environment, simplifying the code.
My experience it that Java IDEs are better than C++ IDEs in providing the features I benefit most from. C++ tends to be too unstructured and unpredictable to provide the same level of error detection, resource location, and code completion that I found with JBuilder. -- Regards, Steven
On Sat, 25 Sep 2004 06:38:56 -0400
"Steven T. Hatton"
I can't speak with unquestionable authority on this, but I believe you might find Java has it's own hidden legacies. Namely Small Talk and Lisp (spelled Emacs). It certainly has some history. The Java language is very C-ish by design. A few things were changed: integer sizes are explicit floating point size is explicit. char is not an integer. Dynamic memory allocation is controlled in that you can only allocate an object, and there is no explicit free as this is done automatically. The language is object oriented by design. I see absolutely no Lisp in this at all.
Threads. I consider this a plus for Java since the programmer does not need to consider different threading models.
Until very recently, I was of the opinion that C++ was better off without standardizing threading. That allows for other threading 'standards', models, or whatever you want to call them to be created to suite specific needs, or simply to allow for more innovation. Sure with Java you pretty much know what you are dealing with in all cases. But that doesn't allow for much flexibility.
There is much debate over threads. What I find problematical is not the
lack of a threading paradigm, but the lack of recognition by the
standards committees. The problem is that threadsafeness is very
platform dependent. Certainly, the STL is not really a good candidate
for being threadsafe, but the entire C runtime library could be made
threadsafe on all platforms, but that also opens a whole can of worms
for the standard people. Where Java (and ADA) win on this case, is that
the language itself specifies the thread paradigm, and the underlying
run time system must implement it.
http://people.redhat.com/drepper/tls.pdf
This is talking about Thread Local Storage. TLS is used by Microsoft and
by other vendors to provide a per-thread local storage. Vendors must
come up with a way that they can specify TLS in the soruce code, which
mandates an extension to the compiler and language. There are some
advantages to TLS in that it provides a per-thread static storage. I
also find that the performance cost of TLS is very high because of the
mechanisms used to access it. A more portable way, is to have the parent
allocate a per-thread context, and pass that context to the thread via
the pthread_create. The problem with this approach is that the
per-thread context must be globally visible to all other threads, and a
misbehaving thread can cause problems. Normally I create an array (or
other data structure) containing the thread id, thread number (locally
defined), and possibly some other information used by the thread.
--- standards issues
The problem that a language has by specifying a paradigm like threads,
is that there is an independent standards committee working on threads.
C++ has the additional problem in that there needs to be a certain
amount of C compatibility, and they need to coordinate with the C
committee also to some extent. By Java stating its own thread paradigm,
it forces its threading standard to be implemented on all platforms.
Whether it uses the POSIX or other model underneath is not really an
issue. You are also guaranteed that the standard classes are threadsafe.
--
Jerry Feldman
On Saturday 25 September 2004 14:34, Jerry Feldman wrote:
On Sat, 25 Sep 2004 06:38:56 -0400
"Steven T. Hatton"
wrote: I can't speak with unquestionable authority on this, but I believe you might find Java has it's own hidden legacies. Namely Small Talk and Lisp (spelled Emacs).
It certainly has some history. The Java language is very C-ish by design. A few things were changed: integer sizes are explicit floating point size is explicit. char is not an integer. Dynamic memory allocation is controlled in that you can only allocate an object, and there is no explicit free as this is done automatically. The language is object oriented by design. I see absolutely no Lisp in this at all.
Garbage collection, byte compiling, running on a VM, no explicit pointers, and I'm confident other features have their origins in Emacs Lisp. If you look closely you may even discover that an object is pretty much just a specialized list.
The problem with this approach is that the per-thread context must be globally visible to all other threads, and a misbehaving thread can cause problems. Normally I create an array (or other data structure) containing the thread id, thread number (locally defined), and possibly some other information used by the thread.
I was under the impression that that's pretty much what's going on with the TLS model discussed in Drepper's paper. There is a global set of indexes mapped to the running threads. The only thing preventing threads from stepping on eachother's storage is good behavior. I believe the main emphasis of the TLS model is to provide a way to maintaining per-thread 'static' storage for each function that uses static variables.
--- standards issues The problem that a language has by specifying a paradigm like threads, is that there is an independent standards committee working on threads. C++ has the additional problem in that there needs to be a certain amount of C compatibility, and they need to coordinate with the C committee also to some extent. By Java stating its own thread paradigm, it forces its threading standard to be implemented on all platforms. Whether it uses the POSIX or other model underneath is not really an issue. You are also guaranteed that the standard classes are threadsafe.
Not everything in Java is thread safe. I know some of the collection objects need to be wrapped in some kind of thready synchronizing class in order for them to be used safely in concurrent programming. -- Regards, Steven
On Sun, 26 Sep 2004 01:11:56 -0400
"Steven T. Hatton"
I was under the impression that that's pretty much what's going on with the TLS model discussed in Drepper's paper. There is a global set of indexes mapped to the running threads. The only thing preventing threads from stepping on eachother's storage is good behavior. I believe the main emphasis of the TLS model is to provide a way to maintaining per-thread 'static' storage for each function that uses static variables.
Since all threads run in the context of the parent, virtually any memory used by an individual thread could be stepped on by another thread, including stack. TLS provides th appearance of static storage that can be set up at compile-time, and is relatively thread-private. The alternative mechanism that I mentioned is a global data structure, that could be easily trashed by misbehaving threads. -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Sun, 26 Sep 2004 01:11:56 -0400
"Steven T. Hatton"
Garbage collection, byte compiling, running on a VM, no explicit pointers, and I'm confident other features have their origins in Emacs Lisp. If you look closely you may even discover that an object is pretty much just a specialized list. First, running in a VM is shared by nearly every interpretive language. Lisp, of course, is one of the oldest computer languages with its origin going back to the 1930s. One also must remember that Gosling was the author of the Gosling, or Carnegie-Mellon Emacs. Compiling to an intermediate language has been around for a while also. We can also look at Algol and see some Algol in Java. But, the basic language is essentially C with some fixes. Similar to C++, Java has constructors and overloading, but also lacks destructors, which are somewhat unnecessary.
--
Jerry Feldman
On Sunday 26 September 2004 07:56, Jerry Feldman wrote:
On Sun, 26 Sep 2004 01:11:56 -0400
"Steven T. Hatton"
wrote: Garbage collection, byte compiling, running on a VM, no explicit pointers, and I'm confident other features have their origins in Emacs Lisp. If you look closely you may even discover that an object is pretty much just a specialized list.
First, running in a VM is shared by nearly every interpretive language. Lisp, of course, is one of the oldest computer languages with its origin going back to the 1930s. One also must remember that Gosling was the author of the Gosling, or Carnegie-Mellon Emacs. Compiling to an intermediate language has been around for a while also. We can also look at Algol and see some Algol in Java. But, the basic language is essentially C with some fixes.
I don't disagree that it superficially is very much like C with much in common with C++ as well. It is blatantly clear that the Java designers were looking at C++'s object model when they created their own. Even where they deviated from C++, it was often a conscious departure from the model they were following. They also took some things from Small Talk. Since I know far less about ST than C++, I cannot say much here other than that, I believe they took the concept of a universal base class (UBC) from Small Talk.
Similar to C++, Java has constructors and overloading, but also lacks destructors, which are somewhat unnecessary.
To really understand one significant philosophical difference between Java and C++ you need to think in terms of Stroustrup's notion of invariants. The I first understood what he was really trying to say when I read this article on exception handling: http://www.research.att.com/~bs/eh_brief.pdf C++ ,in comparison to Java, assumes the programmer will pay closer attention to resource management in general. Thinking about efficiency is central to the use of C++. At first I thought that was a bit archaic in comparison to the Java way of thinking. Now I have come to believe it's a good thing. I'm currently working with 3D graphics. On a high-end modern system the efficiency of the program still counts for a whole lot. I believe this will be true for many other areas where it may seem less obvious, such as searching large bodies of data. -- Regards, Steven
I don't disagree that it superficially is very much like C with much in common with C++ as well. It is blatantly clear that the Java designers were looking at C++'s object model when they created their own. Even where they deviated from C++, it was often a conscious departure from the model they were following. They also took some things from Small Talk. Since I know far less about ST than C++, I cannot say much here other than that, I believe they took the concept of a universal base class (UBC) from Small Talk. This is most likely true. C++ was essentially built upon legacy C where
On Sun, 26 Sep 2004 13:15:54 -0400
"Steven T. Hatton"
On Sunday 26 September 2004 14:32, Jerry Feldman wrote:
On Sun, 26 Sep 2004 13:15:54 -0400
"Steven T. Hatton"
wrote: I don't disagree that it superficially is very much like C with much in common with C++ as well. It is blatantly clear that the Java designers were looking at C++'s object model when they created their own. Even where they deviated from C++, it was often a conscious departure from the model they were following. They also took some things from Small Talk. Since I know far less about ST than C++, I cannot say much here other than that, I believe they took the concept of a universal base class (UBC) from Small Talk.
This is most likely true. C++ was essentially built upon legacy C where the designers of Java had no legacy language to support. They decided to enforce a strict OO environment. they also decided to avoid multiple inheritances as well as operator overloading.
I believe a better way of putting that last sentence would be, "They also resigned themselves to forego multiple inheritances as well as operator overloading." I believe the latter has always been a regret of Gosling. The reason the former was not doable is because if you have a UBC, you have a diamond DAG at every node in your inheritance diagraph other than the root. It's unfortunate that C++ programmers don't fully understand the merits of pure virtual interface classes. Many good aspects of Java's design come from a solid understanding a appreciation of Stroustrup's design philosophies. In many cases Java programs do a better job of realizing Stroustrup's intentions than comperable C++ programs. Much of this is not due to the language differences, per se. It's due to a lack of understanding on the part of C++ programmers...And the fact that C++ can get a bit unwieldy with all its #include directives in conjunction with the use of pure virtual functions and MI. This is an interesting animal: http://digitalmars.com/d/index.html -- Regards, Steven
On Sun, 26 Sep 2004 14:44:07 -0400
"Steven T. Hatton"
I believe a better way of putting that last sentence would be, "They also resigned themselves to forego multiple inheritances as well as operator overloading." I believe the latter has always been a regret of Gosling. The reason the former was not doable is because if you have a UBC, you have a diamond DAG at every node in your inheritance diagraph other than the root.
It's unfortunate that C++ programmers don't fully understand the merits of pure virtual interface classes. Many good aspects of Java's design come from a solid understanding a appreciation of Stroustrup's design philosophies. In many cases Java programs do a better job of realizing Stroustrup's intentions than comperable C++ programs. Much of this is not due to the language differences, per se. It's due to a lack of understanding on the part of C++ programmers...And the fact that C++ can get a bit unwieldy with all its #include directives in conjunction with the use of pure virtual functions and MI.
This is an interesting animal: http://digitalmars.com/d/index.html Interesting. We are essentially on the same page. -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Wednesday 15 September 2004 02:35 am, Steven T. Hatton wrote:
Q: This question is for people with both C++ and Java experience. Do you believe there are features of Java that C++ could also have, and would benefit from?
I have several thoughts on this subject, not all related to your question... 1. The whole hardware-vs-human cost equation has changed since the C language was created. As a result, C (and therefore C++) is forever biased towards programmer-managed efficiency. On the other hand, Java uses a garbage collector which is biased towards system-managed efficiency. There are good and bad points to both. Yes, there are garbage collectors available for C/C++, but that doesn't change the mindset of most C/C++ professionals. 2. Java is more standardized on the platforms it supports than C++. Hence the "write once, run anywhere" mantra. Perhaps this is because Java comes from a single vendor whereas C++ is an open standard implemented by multiple vendors. Again, there are good and bad points to both. 3. Java provides an extremely well-rounded library and set of tools. And the cost for doing all this is zero or near-zero. It can be argued that C++ has various system libraries at its disposal, but those aren't guaranteed as part of the language. 4. Java offers an Enterprise Edition (J2EE) as an add-on. With it, you can accomplish web development that utilizes the same business logic or data access code used in non-web programs. To my knowledge, There is no multi-platform equivalent for C++. 5. Java development occurs at a hgher level of abstraction than C++. This is because develpers don't get bogged down with language-specific features as often (namely pointers, references, indirection, function tables, etc...). 6. C/C++ has the advantage of portable code. Thanks to gcj, native compilation for Java is possible, even on OS's that Sun doesn't support. The binaries tend to be a little bigger than those compiled with gcc (up to 20% in my experience), but that has been an acceptable trade-off. I think there are performance comparisons between gcc and gcj compiled apps on the web. 7. Its ironic that C++ is a 'standardized' language, yet C++ implementations are so widely different. And vendor extensions (notably from Microsoft) make the problem of compatibility even more difficult. Best of luck in your endeavors.... Chris
Steven T. Hatton wrote:
Q: This question is for people with both C++ and Java experience. Do you believe there are features of Java that C++ could also have, and would benefit from?
Yes: 1. mutexed blocks (why does java call them 'synchronized' when they can't happen at the same time?) 2. garbage collection. 3. swing. Of course, you can do all of this in C++ already, but java handles most of this much better and the first two could be built in to the language. I suspect it's more likely that java will gain features from C++, like the generics in 1.5.0 and get nearly as fast with gcj. C++ could benefit from features of most languages, except of course Visual Basic, which is the supreme absence of good. -- JDL
On Wed, 15 Sep 2004 18:13:54 +0100
John Lamb
Steven T. Hatton wrote:
Q: This question is for people with both C++ and Java experience. Do you believe there are features of Java that C++ could also have, and would benefit from?
Yes: 1. mutexed blocks (why does java call them 'synchronized' when they can't happen at the same time?) Mutex is simply one terminology for a "synchronization primitive". Semaphore is also used. There are other terminologies used in various other implementations of things.
2. garbage collection. This is a good thing for an application programming language, but a bad thing for a system implementation language. IMHO, C's malloc(3) and Free(3) paradigms should remain. However, I consider the lack of a garbage collector in the C++ language a limitation. 3. swing. A very good thing. +++ for Java and Sun.
--
Jerry Feldman
participants (4)
-
Chris Freyer
-
Jerry Feldman
-
John Lamb
-
Steven T. Hatton