Perspectivesfrequal.com

Reasons to prefer Java over C++

Several flaws in C++ make Java a preferable language:
  • Comment Double-Maintenance: C++ introduces a comment double-maintenance issue by placing the prototype and implementation of methods in separate files (.h and .cpp). If you want to document what your public function does in both the header and cpp file, you have to include the comment in both places. In Java, there is just one place to provide the method description, in the .java file where it is declared.
  • Inconsistent String Use: C++ is hampered by legacy support of C standard libraries that use char* instead of the standard string class. This requires calls to c_str() every time a char* is needed. Java has no such legacy burdens, and all libraries use String consistently when dealing with strings.
  • Easier debugging due to complete stack traces with line numbers
  • No mysterious failures or randomly changing variables due to array overruns, mismatched object files, and other pointer problems.
  • No string corruption due to misplaced NUL characters or use of older C++ libraries that require manual string termination.
  • No memory model means that code that multithreaded code that works on one system may not work on another. Java, by contrast, has a well-defined memory model so that you are guaranteed that code that works on one platform will work on another, even if that platform has a different hardware and operating system
  • In C++ it is possible to compile code that does comparisons on iterators that fails at runtime. The "<" operator is sometimes legal, sometimes not. Java sidesteps this whole minefield by making the Iterator interface safe: the hasNext() method returns a simple, safe boolean that cannot be used erroneously.
  • When is true not equal to true? When you're using C++! If you leave a boolean uninitialized, and the value is greater than 1, then when the compiler uses the cmp x86 assembly instruction to compare the two, they are reported to be unequal. true != true! Java guarantees the initialization of all variables to default values so you'll never encounter this painful problem.
  • No need to worry about structure packing in Java. By allowing developers to choose structure packing , C++ costs developers time spent making false optimizations and finding bugs caused by mismatched alignments and platform-specific packing specifiers. In Java, the virtual machine worries about packing and alignment, and I've never heard anyone complain about its absence.

Last modified on 19 Dec 2006 by AO

Copyright © 2024 Andrew Oliver