Comparison of Java to C Plus Plus

Template:Title

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

Design aims

Both Java and C++ were designed to support general-purpose application programming. However, unlike Java, C++ was also designed to support systems programming. Because of this, C++ has several design principles that Java does not share, such as:

  • the programmer should not have to pay (efficiency-wise) for language features he does not use;
  • the language should trust the programmer to the extent that it should allow the programmer to override the type system, perform manual memory management, perform pointer manipulation, and even execute arbitrary assembly language code.

These principles combined with C++'s C heritage are responsible for most concrete language differences between Java and C++.

Language features

  • Java has automatic garbage collection. C++ does not. Automatic garbage collection is available via various non-standard libraries in C++, but it is not part of the language.
  • Java has considerably simpler syntax than C++.
  • C++ is normally compiled directly to machine code which is then executed directly by the operating system. Java is normally compiled to byte-code which is then executed by the Java virtual machine.
  • Due to the crudeness of some C++ language features (eg. unchecked array access, raw pointers), programming errors can lead to low-level buffer overflows, page faults, and segmentation faults. In Java, such errors either simply cannot occur or are detected by the Java virtual machine and reported to the application in the form of an exception.
  • C++ is mostly backwards compatible with C, and C libraries (such as the operating system API) are usually directly accessible from C++. In Java, access to native operating system and hardware functions requires the use of the Java Native Interface.
  • Java has a considerably larger standard library than C++. The C++ standard library only provides components that are relatively general purpose, such as strings, containers, and I/O streams. The Java standard library includes components for networking, graphical user interfaces, XML processing, logging, database access, cryptography, and many other areas.
  • Java has generics. C++ has templates. See Comparison of generics to templates.
  • C++ is generally more time and space efficient than Java, especially in digital signal processing and other low-level or arithmetic-heavy code.
  • C++ features operator overloading. Java does not, although certain operators were overloaded for standard library classes such as String.
  • Both Java and C++ distinguish between native types (these are also known as "fundamental" or "built-in" types) and user-defined types (these are also known as "compound" types). However, in C++ this distinction is far less extensive.
  • C++ supports multiple inheritance of arbitrary classes. In Java a class can derive from only one class, but in addition a class can implement multiple interfaces.
  • Java explicitly distinguishes between interfaces and classes. C++ does not have interfaces, but has a more general notion of classes which makes it possible to define classes with semantics equivalent to Java interfaces.
  • Java and C++ use different idioms for resource management. Java relies mainly on garbage collection, while C++ relies mainly on the Resource Acquisition Is Initialization idiom. This is reflected in several differences between the two languages:
    • In C++ it's common to allocate objects of compound types as local stack-bound variables which are destructed when they go out of scope. In Java compound types are always allocated on the heap and collected by the garbage collector.
    • C++ has destructors, Java does not.
    • Java has finalizers, C++ does not.
    • Java features a finally clause in try statements, C++ does not.
  • The C++ type system incorporates the notion of const-ness. Java has no equivalent concept.
  • In Java, whether a method parameter is passed by value or by reference is determined by whether or not the type of the parameter is native. In C++ whether a function parameter is passed by value or by reference is up to the writer of the function and becomes part of the function's type signature.
  • C++ allows namespace level constants, variables, and functions. Java does not.
  • C++ tends to adapt more to the environment and machine it runs on:
    • The ranges of C++ fundamental types is platform dependent. Java native types are fixed.
    • The encoding of string and character literals in C++ is platform dependent. The encoding of string and character literals in Java is fixed.
  • In C++, pointers can be manipulated directly as memory address values. Java references on the other hand can only refer to objects or "null". Another way to look at this is that in C++ pointers are actual first-class objects, while in Java references are merely syntactic entities. An illustrative example of this is that C++ one can construct pointers to pointers, which have no Java equivalent.
  • C++ features goto. In Java "goto" is a reserved keyword that is barred from use.
  • In Java, bounds checking is implicitly performed for all array access operations. In C++, array access operations on native arrays are not bounds-checked, and bounds checking for random-access element access on standard library collections like std::vector and std::deque is optional.
  • C++ features inline assembler. Java has no equivalent feature.
  • Java features "static initializers"; blocks of code that are executed when a class is loaded. C++ has no equivalent feature.
  • Java and C++ use different techniques for splitting up code in multiple source files. Java uses an advanced package system, C++ uses a header file inclusion system.
  • C++ has a preprocessor. Java has no equivalent feature.

See also: Comparison of C# to Java

External references

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools