This page highlights some of the differences between Java and C++, giving both features that appear in one language but not the other, and also features that are present in both but work differently in each. However extensive the list is, or becomes, it should never be regarded as complete!

This material has been compiled from several sources. Bruce Eckel's first edition of Thinking in Java has a particularly useful Appendix B (whose contents are omitted from the second edition). As Eckel points out, even though C++ and Java seem to be quite similar at first, there are a (perhaps surprising) number of differences. Where Java differs from C++, the differences were meant to be improvements in the language, and Eckel argues that understanding the differences will help you appreciate why Java is such a beneficial language. [Interestingly, that's the word he uses: "beneficial", not "better".]

Features found in Java but not in C++

  1. Use of 16-bit Unicode for characters, including characters used for writing programs
  2. Run-time interpreter (C++ is generally a compiled language.)
  3. Support for dynamic and automatic compilation, loading and execution of classes found on the CLASSPATH
  4. Support for automatic garbage collection (In C++ garbage collection is the programmer's responsibility.)
  5. Support for Web "applets"
  6. Support for platform-independent GUI programming
  7. Support for event-driven programming
  8. Support for multithreading and synchronization
  9. Support for networking
  10. Support for security via a security manager
  11. Support for native language interface programming (keyword: native)
  12. A single-inheritance hierarchy with "Object" as the root superclass (keywords: extends and super)
  13. Everything must be in a class, including all method definitions.
  14. Interfaces (keywords: interface and implements)
  15. Code organization by packages (keywords: package and import)
  16. A "package" or "friendly" access protection category (but no corresponding keyword)
  17. All object creation at run-time via new
  18. A byte data type
  19. Fixed-size primitive types across all platforms
  20. Wrapper classes for primitive types
  21. The right-shift operator >>>, which uses zero extension, i.e., regarless of the sign, zeroes are inserted at the higher order bits when the shift takes place.
  22. Automatic virtual (in the C++ sense) instance methods
  23. Arrays and strings as "first-class" objects, and use of + for conversion to String as well as + and += for string concatenation
  24. Final methods that cannot be redefined and final classes that cannot be extended (inherited from)
  25. Automatic initialization of primitive class members to zero and of member object references to null.
  26. Field initializers and static initialization blocks
  27. Built-in support for comment documentation, so that a source file can contain its own documentation, which is stripped out and formatted into HTML by the javadoc utility
  28. Inner class objects that secretly keep a handle to the object of the outer class that was involved in the creation of the inner class, permitting the inner class object to access members of the outer class object without qualification
  29. Methods that throw or specify "checked" exceptions, i.e., exceptions that must be dealt with (or at least recognized and not dealt with) by the programmer
  30. Object variables and parameters are always references (or "handles")

Features found in C++ but not in Java

  1. The need to recompile so that the program will run on different platforms
  2. Implementation-dependent features such as the size of primitive data types, conditional compilation depending on the platform, and pragma directives
  3. Pointers, pointer arithmetic, pointers to members, and the operators -> and ->*
  4. A preprocessor, the #include directive, and header files
  5. Global functions unattached to any class
  6. Operator overloading
  7. The need to free memory manually with the delete operator
  8. Multiple inheritance (i.e., inheriting from multiple base classes)
  9. The keywords (and corresponding concepts) struct, union, typedef, enum, inline, register, unsigned, friend, and template
  10. The keyword virtual (C++'s "virtual" is the default in Java)
  11. The comma operator (,) the scope resolution operator (::), and the sizeof operator
  12. Templates
  13. Optional (default) parameters and variable-length parameter lists
  14. User-defined type conversions
  15. extern declarations
  16. Destructors, with the destructor name depending on the class name
  17. Implicit copying of objects (Java has no "copy constructors".)
  18. Initialization list for constructors
  19. Reference variables and reference parameter declarations
  20. Super classes that are protected or private
  21. Bit fields (which are very seldom used in C++, or even in C)
  22. Unsigned integer data types
  23. The abiltiy to pass a function (method) into another function (method) as a parameter

Features that appear different, or work differently, in Java and C++

  1. Java, being interpreted, runs more slowly that C++, which is generally compiled (up to 20 times slower). Newer just-in-time (JIT) compilers are improving this situation, but for raw speed C++ is still the best. Also, C++ has direct access to the hardware in ways that Java does not, though Java can use "native methods" (keyword: native) written in other languages such as C or C++ in critical situations.
  2. Instead of an "absolute entry point" main, as in C++, each class in Java may have its own main to run and test that particular class.
  3. Java uses exclusively true and false for boolean values, and cannot use any non-zero value for true and zero for false as is done in C++ (and C).
  4. Java requires explicit casting from int to char.
  5. Each class member in Java must have its own explicit access specifier; otherwise the member is deemed to have "package" accessibility (unlike C++ where the default is "private").
  6. The Java % operator works for floating-point values as well as integer values.
  7. Java does not use the keywords const or goto (though they are in fact keywords).
  8. Java has no default return type for methods (and, in fact, modern C++ compilers will warn you if you do not explicitly supply a return type for a function).
  9. Resolution of a call to an overloaded method is very different from that in C++.
  10. Java classes use finalize() methods rather than destructors. (Each class has a finalize() method that is called automatically by the garbage collector.)
  11. The keyword unsigned does not exist in Java, and all integer types use signed twos complement internal representation
  12. Generic programming in Java is accomplished using the Object class, rather than the void* or the templates of C++.
  13. In addition to the >> operator, Java also supports the >>> operator.
  14. Java has access to only a predefined set of environment variables through system properties.
  15. Java type compatibility can be done through both class extension and interface implementation.
  16. Name hiding works differently. "If a Java base class has a method name that's overloaded several times, redefining that method name in the derived class will not hide any of the base-class versions." (Eckel, 2nd Ed., p. 286)
  17. In Java there is no required semi-colon at the end of a class definition, like there is in C++.
  18. There are no class declarations in Java, only class definitions. Hence there are no "forward declarations" of the kind found in C++. (The Java compiler is "smarter" than the C++ compiler, and permits methods to be called before they are defined.)
  19. Java uses the dot operator (.) exclusively, while C++ uses both the dot operator and the scope resolution operator (::).
  20. Type-checking is generally more strict in Java than in C++. For example, conditional expressions must be boolean (i.e., they cannot be integral, as C++ permits).
  21. In C++ you can have a statement like x+y; if you want, just for the side effects, but you can't do this in Java.
  22. Quoted strings are automatically converted to String objects. (There are no "C-strings" in Java.)
  23. In Java you use a package for the same purpose you would use a namespace in C++.
  24. In C++ all objects created on the heap are (or at least, should be) destroyed, but in Java not all objects are necessarily garbage collected. Thus the programmer must be careful to create any required cleanup methods and to invoke explicitly any such methods for both base classes and derived classes, as well as for member objects in those classes.
  25. The boolean data type in Java corresponds to bool in C++.
  26. Java allows break/continue to a labeled outer loop.
  27. The Java protected keyword means "accessible" to inheritors and to other classes in this package, while in C++ the protected keyword means "accessible to inheritors only" (for which there is no longer any equivalent in Java, though there was earlier).
  28. Although in C++ you can specify public, protected or private inheritance, in Java you cannot do this and so inheritance in Java does not change the protection level of the members of the base class.
  29. In Java, overridden methods in a derived class cannot reduce the access level of the corresponding method in the base class. For example, if a method in the base class is public and you override it in a derived class, the overriding method must also be public, or you will get a compile-time error.
  30. Java contains standard libraries that help with solving certain tasks that in C++ require non-standard third-party libraries.
  31. Exception handling is, in general, better designed and more robust in Java than in C++.
  32. Unlike in C++, in Java you cannot arrange for explicit calls to more that one other constructor in a given constructor.
  33. Java insists that boolean operations always be performed left-to-right, while this is not necessarily true in C or C++.
  34. The Java compiler does not insist that a program be ordered in such a way that each method is defined before a call to that method appears, while the C++ compiler does.
  35. In a Java constructor you cannot arrange for explicit calls to more than one other constructor, in the way you can in C++. This is because any such call must be the first statement in the current constructor, and there can be only one such statement.
  36. A variable declared in an outer scope may not be "hidden" by redeclaring it in an inner scope.

Some features that are common to C++ and Java

  1. Comments using // as well as comments using /* */
  2. The names of most primitive data types (char, short, int, long, float, double)
  3. All looping and selection constructs (for, while, do..while, if..else, switch, ?:)
  4. Method overloading in Java and function overloading in C++