Encyclopedia > Comparison of Java to Cplusplus

  Article Content

Comparison of Java to Cplusplus

This is a comparison of the Java programming language to the C++ programming language.

Table of contents

Advantages of Java

  • The extensive and standardized Java API makes it considerably easier to program in Java than in C++. The standard Java API included with every modern JVM includes built in, standardized libraries for TCP/IP network sockets and services, XML parsing, logging, SQL database access, cryptography, and many other areas. C++ libraries which perform similar functions to the Java API are usually available, but there are typically several competing and mutually incompatible versions with overlapping functionality which cannot easily be used together.
  • Garbage collection facilitates programming and the safety of program execution considerably as programmers do not need to dynamically allocate memory and then remember to free it later.
  • Java is easier to teach and learn because of its streamlined syntax. It inherits less baggage from the C language, most notably in bypassing the need to deal with pointers.
  • The lack of pointers means that buffer overflow bugs (and, consequently, security exploits) are practically impossible in Java. Built in bounds checking prevents an errant program from overwriting the end of a memory buffer or assigning the value of an incompatible data type to a variable.
  • These built in checking mechanisms in Java make it a more robust software environment. On the other hand, they also make Java execution slower (see below).
  • Java can be programmed for multiple plaforms with little regard towards platform-specific characteristics like hardware data types, floating point implementations, or OS libraries. Java programs are compiled into binary bytecode which will execute properly on any standards-compliant JVM, on any architecture, without modification.

Disadvantages of Java

  • Access to native operating system and hardware functions requires a non-Java access library be coded to the JNI (Java Native Interface) API specification. Java programs cannot directly access such services.
  • No compile-time "template" generic containers. (this is slated for inclusion in version 1.5 due to popular demand.)

Advantages of C++

  • C++ provides easy access to the operating system API and other low-level resources.
  • Because the native code compilers for C++ are more mature than those for Java, it is often possible to write more efficient digital signal processing and other arithmetic-heavy code in C++ than in Java.
  • You don't incur overhead for features you do not use. For example, automatic garbage collection and mandatory virtual members make Java performance unsuitable for some applications.
  • It's possible to fit a C++ runtime library into a small memory and storage footprint, letting C++ code run on tiny embedded systems where even Java 2 Platform, Micro Edition won't run.
  • The C++ language is defined by an open ISO standard, while Java is proprietary.
  • C++ can be compiled for more platforms than are supported by the Java VM (though porting to a different architecture often requires extensive refactoring of the code).
  • C++ has a much more robust model for enforcing constants, as well as provides stricter and more specific casting keywords.

Disadvantages of C++

  • The presence of hardware pointers makes it easy to write a program that will inadvertently overwrite code in memory with data. This leads to buffer overflow security holes.
  • The programmer must specifically free any dynamically allocated memory, or else create a memory leak which can eventually exhaust all available system memory.
  • C++ takes longer to learn and the syntax is less forgiving.

Differences between the languages

  • C++ has operator overloading. Java discards these as unnecessary, though many hold this position in contention.
  • All objects in Java are pass-by-reference, and are dynamically allocated.
  • Java constants (the "final" keyword) are only enforced within class scope.
  • C++ has flexible support for pointer manipulation, while Java only allows one to assign object references.
  • There is no "goto" in Java (though it is a reserved keyword and barred from use.)
  • C++ supports multiple inheritance while Java uses "interfaces". They each have their advantages and disadvantages. Multiple inheritance is often more logical and representative of how one conceptualizes a problem. However it also complicates member overloading and inheritance, and even its proponents only recommend its use sparingly. Java interfaces significantly simplify its single inheritance model syntax, but can result in larger and less intuitive source code.

External References:

All Wikipedia text is available under the terms of the GNU Free Documentation License

  Search Encyclopedia

Search over one million articles, find something about almost anything!
  Featured Article
Canadian Charter of Rights and Freedoms

... Canada's federal government and certain of Canada's provincial governments minority language education rights: generally, French and English minorities in every province ...

This page was created in 28 ms