Index: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Home > Comparison of Java to C Plus Plus
Articles to be merged
- This article should be merged with
Java programming language & C++ programming language.
This is a comparison of the Java programming language to the C++ programming language.
1 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, standardized 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 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).
- 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.
2 Disadvantages of Java
- Access to native operating system and hardware functions requires a non-Java access library be coded to the JNI ( Java Native InterfaceThe Java Native Interface JNI is a programming framework that allows Java code running in the Java virtual machine (VM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in o) API specification. Java programs cannot directly access such services.
- No compile-time "template" generic containers, until recently. Version 1.5 (Java 2 version 5) has generics, although there are differences from C++.
- No support for multiple inheritance. Instead, Java supports interface classes that cannot implement any methods but otherwise behave like abstract base classes. Multiple inheritance allows better program organization and avoids code duplication. It was not included in Java because multiple inheritance also makes creating spaghetti codespaghetti looks twisted and tangled, which is where the name for spaghetti code comes from. Spaghetti code is a pejorative term for a computer program code with a complex and tangled control structure, especially one using many GOTOs, exceptions, threads, easier.
- Java does not support destructors. This makes impossible to use automatic resource management. Instead any used operating system resources have to be released by hand. (e.g. by calling Close() methods). This makes exception handling some times quite complicated and hard to get it right without leaking resources.
Read more »