Next Previous Contents

24. Appendix B C++ v/s Java

The students in Universities can debate the pros and cons of C++ and Java. Just for a debate sake, given below are some points discussing C++ versus Java:

  1. Generic Programming: C++ has an extremely powerful template meta language, that allows a programmer to reuse many algorithms without paying neither performance, nor conveniences like type safety, for it. The somewhat equivalent for java, which is "all classes inherit from Object", so that you can have vectors, lists, cloneable, etc., is at the same time cumbersome and dangerous, beacuse it forces programmers to duplicate code possibly introducing nasty minor bugs -the ones that results in intermitent failures, the worsts of all- doing that re-implementations. Conclusions are that :
  2. Multiple Inheritance: Java doesn't allow multiple inheritance: While it allows a class to implement as many interfaces as it wants, a new class can not make use of "arquetipical" implementations of the interfaces. What happens all the time, is that the programmer simply produces such implementation of the interface, and then copies and pastes that into each new class that implements it. The problem with that is the same as above: minor bugs leading to intermitent crashes, and also the problem that once you decide to change the interface.... you have to browse in all your code for each implementation to adapt it to the change.... that is the most anti Object Oriented practice you could find. Instead, in C++ you simply inherit some virtual methods from the "interface class", and just re-implement the really important pure virtual methods of the base class (the real interface). In that way, you can provide directly in the base class some services (methods) for implementers of its pure virtual ones. Some computer intellectuals say that multiple inheritance is an error. It is a very important tool for describing the reality to the computer (design).
  3. Operator overloading: Suppose you are developing some numerical recipes, and you work with matrices, where you have to transpose, invert, add, multiply by scalar, etc. So, honestly, what do you prefer?: (A, B, C, D matrices and scale a double):
            A = scale * B^-1 *  (C + D)  * ~B   
            (in C++ overloading * twice, +, ^, and ~ operators)
            to this Java equivalent :
            A.assign(B.invert().scalar_product(scale).product(C.add(D)).product(B.transpose())
    

    By the way, these are real life examples.
  4. This differentiates C++ from Java: C++ is a very rich language, and yet, easily extensible (but not mutable, as Stroustrup says), but in the other hand, Java is a very poor language (altough it is very useful), and not extensible at all. If you need something outside what normal Java can provide, you have no alternative - use JNI. Consequently, many many important programming techniques or design patterns can be naturally expressed in C++ only.
  5. Some more points .... to come in future..


Next Previous Contents