Comparison Of Java To C Plus Plus

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

Advantages of Java

  • The extensive and standardized Java API makes it considerably more convenient to program in Java than in C++. The standard Java API included with every modern JVM includes built in libraries for TCP/IP network sockets and services, the Swing library for generic windowing system access, 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 often multiple competing libraries with similar functionality. These libraries are also usually not bundled with the base C++ system. As a result, many programmers find using the Java API more convenient and consistent than using an equivalent set of C++ libraries.
  • Garbage collection frees programmers from the need to manually allocate and release memory. This is usually an advantage, as it avoids errors and makes programmers more productive, although there are situations in which garbage collection is a burden.
  • Java is easier to teach and learn because of its relatively simple syntax. It inherits less baggage from the C language, most notably by not providing pointers.
  • The lack of pointers means that buffer overflow bugs (and, consequently, security exploits) are practically impossible in Java. Array bounds checking prevents a program from writing past the end of an array, and runtime type checking ensures that it is impossible to assign a value of an incompatible type to a variable. These built in checking mechanisms tend to reduce bugs, although they come with a runtime cost (see below).
  • The fact that compiling to machine code (which most VMs do now) is done at run time means that as better compilers are developed, the same bytecodes will often perform better automatically. This is in contrast to C++, where taking advantage of a new compiler optimization requires recompiling.
  • 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.
  • Although the language specification is controlled by Sun Microsystems, the specification of both the language and the platform are freely distributed. This is opposed to the C++ standard, which must be purchased from ISO, a disadvantage for students and others who do not use the language for commercial gain.
  • Java allows the definition of a static initializer, a block of code that is executed when a class is loaded. This is possible in Java because of its carefully specified class loading semantics, whereas in C++, class loading is treated as an implementation-specific problem.

Disadvantages of Java

  • Access to native operating system and hardware functions requires the use of JNI (Java Native Interface), which cannot be implemented in Java. Java programs cannot directly access such services.
  • No compile-time "template" generic containers, until recently. Version 1.5 (Java 2 version 5) has generics, and although they allow the implementation of statically type-safe generic containers, they are more limited than C++ templates and do not allow template metaprogramming. See Comparison of generics to templates.
  • No support for multiple inheritance. Instead, Java supports interface classes that cannot implement any methods but otherwise behave like abstract base classes. Some people believe that multiple inheritance allows better program organization and avoids code duplication and is therefore advantegous. Others believe multiple inheritance makes creating spaghetti code easier. Multiple inheritance also introduces the diamond problem.
  • Java does not support destructors but it provides call to finalize method when the garbage collector destroys the object. This makes it harder to use automatic resource management. Instead any operating system resources that are used have to be released by hand or by implementing finalize method. (e.g. by calling Close() methods). This makes exception handling sometimes quite complicated and hard to get right without leaking resources.

Advantages of C++

  • C++ provides easy access to the operating system API and other low-level resources.
  • Because C++ is designed to compile to native code, native code compilers for C++ are more mature than those for Java. Programs written in C++ typically consume less memory than those written in Java, and are often more efficient as well. This applies particularly to digital signal processing and other arithmetic-heavy code.
  • In C++, the programmer typically incurs little or no overhead for features that they 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, although porting C++ code to a different architecture can require extensive revision of the code.
  • C++ has a much more robust model for enforcing constants. It also provides stricter and more specific casting keywords.
  • C++ provides pointers and inline assembler, which can at times be easier than the high-level workaround.

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 can result in buffer overflow security holes. Modern C++ style discourages the use of pointers; instead, references, stack allocation, and smart pointers are used.
  • The programmer must specifically free any dynamically allocated memory, or else create a memory leak. The risk can be reduced by prudent selection of third-party libraries and the use of features such as STL and smart pointers.
  • C++ takes longer to learn and the syntax is less forgiving. However, the basic syntax of C++ is very similar to that on Java. The differences arise only when using the more advanced features of the language.

Differences between the languages

  • C++ has operator overloading. This was not included in Java because it was believed that operator overloading makes code less readable. While misuse of this feature in C++ can decrease readability, there are also numerous situations, such as in mathematical and scientific programming, where it can significantly improve readability. Because the biggest application area for Java is business software, operator overloading is still considered by the language designers to be unnecessary, though many hold this position in contention. Not all programmers believe that using a word to make a function call is more readable that the use of a symbol (operator). The developers of Java overloaded the operators of the String class, which is believed to increase the readability of string manipulation in Java.
  • Java has one parameter-passing mechanism only: pass-by-value. This includes object references, which has the important effect of allowing the original object to be modified from within the method, and gives the appearance of pass-by-reference. Java objects are not passed at all; only primitives and reference values.
  • Java has no global constants, variables, or functions. However there is little difference between a public static member variable or function and their global counterparts.
  • C++ has flexible support for pointer manipulation, such as creating pointers to primitive data types. Java references can only be assigned to objects.
  • 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.
    • Some degree of multiple inheritance functionality can also be gained by using "inner classes"
  • Templates (C++) and generics (Java) are handled differently. In C++, each instantiation of a template generates its own code. In Java, generics only exist at compile time, which means at run time there is only one copy of the code.
  • C++ allows the allocation of objects on the stack. Java does not; all Java objects are heap-allocated, while primitive data types and object references can be allocated either on the heap or the stack. Allocating objects on the stack in C++ is more efficient than using heap allocation, and removes the need for the programmer to explicitly free the object (it is automatically released when the block is left).
See also: Comparison of C# to Java

External references

 

<< PreviousWord BrowserNext >>
cosmogony
u.s. invasion of afghanistan
gattaca
human genome
farnsworth hirsch fusor
philo farnsworth
adjustable spanner
cape horn
robert mugabe
walvis bay
viscometer
alexander dubcek
anthrax disease
anthrax (band)
pythagorean tuning
the indian trade
acetic acid
meantone temperament
suva, fiji
north american aerospace defense command
the queen of the damned
jacobus henricus van 't hoff
javaserver pages
concordia university
java development kit
jsp
pine (email client)
john of damascus
detection
photodiode
autolysin
biostasis
chitinase
encyclopedia astronautica
lip piercing
301
302
303
irish free state
304
poop
endospore
theodore the studite
caro