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++
-
Use of 16-bit Unicode for characters, including
characters used for writing programs
-
Run-time interpreter
(C++ is generally a compiled language.)
-
Support for dynamic and automatic compilation, loading
and execution of classes found on the CLASSPATH
-
Support for automatic garbage collection
(In C++ garbage collection is the programmer's
responsibility.)
-
Support for Web "applets"
-
Support for platform-independent GUI programming
-
Support for event-driven programming
-
Support for multithreading and synchronization
-
Support for networking
-
Support for security via a security manager
-
Support for native language interface programming
(keyword: native)
-
A single-inheritance hierarchy with "Object" as the
root superclass (keywords: extends and
super)
-
Everything must be in a class, including all method
definitions.
-
Interfaces (keywords: interface
and implements)
-
Code organization by packages (keywords:
package and import)
-
A "package" or "friendly" access protection category
(but no corresponding keyword)
-
All object creation at run-time via new
-
A byte data type
-
Fixed-size primitive types across all platforms
-
Wrapper classes for primitive types
-
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.
-
Automatic virtual (in the C++ sense) instance methods
-
Arrays and strings as "first-class" objects, and
use of + for conversion to String as well
as + and += for string concatenation
-
Final methods that cannot be redefined and final classes
that cannot be extended (inherited from)
-
Automatic initialization of primitive class members
to zero and of member object references to null.
-
Field initializers and static initialization blocks
-
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
-
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
-
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
-
Object variables and parameters are always references
(or "handles")
Features found in C++ but not in Java
-
The need to recompile so that the program
will run on different platforms
-
Implementation-dependent features such as
the size of primitive data types, conditional
compilation depending on the platform, and
pragma directives
-
Pointers, pointer arithmetic,
pointers to members, and the operators ->
and ->*
-
A preprocessor, the #include
directive, and header files
-
Global functions unattached to any class
-
Operator overloading
-
The need to free memory manually with
the delete operator
-
Multiple inheritance (i.e., inheriting from multiple base classes)
-
The keywords (and corresponding concepts)
struct, union, typedef,
enum, inline, register,
unsigned, friend, and template
-
The keyword virtual (C++'s "virtual" is
the default in Java)
-
The comma operator (,) the scope resolution operator (::),
and the sizeof operator
-
Templates
-
Optional (default) parameters and variable-length parameter lists
-
User-defined type conversions
-
extern declarations
-
Destructors, with the destructor name depending
on the class name
-
Implicit copying of objects
(Java has no "copy constructors".)
-
Initialization list for constructors
-
Reference variables and reference parameter declarations
-
Super classes that are protected or private
-
Bit fields (which are very seldom used in C++, or even in C)
-
Unsigned integer data types
-
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++
-
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.
-
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.
-
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).
-
Java requires explicit casting from
int to char.
-
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").
-
The Java % operator works for
floating-point values as well as integer values.
-
Java does not use the keywords const
or goto (though they are in fact keywords).
-
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).
-
Resolution of a call to an overloaded method is very
different from that in C++.
-
Java classes use finalize() methods
rather than destructors. (Each class has a
finalize() method that is called
automatically by the garbage collector.)
-
The keyword unsigned does not
exist in Java, and all integer types use signed
twos complement internal representation
-
Generic programming in Java is accomplished
using the Object class, rather than the
void* or the templates of C++.
-
In addition to the >> operator,
Java also supports the >>> operator.
-
Java has access to only a predefined set of environment
variables through system properties.
-
Java type compatibility can be done through both class
extension and interface implementation.
-
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)
-
In Java there is no required semi-colon at the end
of a class definition, like there is in C++.
-
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.)
-
Java uses the dot operator (.) exclusively, while C++ uses
both the dot operator and the scope resolution operator (::).
-
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).
-
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.
-
Quoted strings are automatically converted to
String objects. (There are no
"C-strings" in Java.)
-
In Java you use a package for the same
purpose you would use a namespace in C++.
-
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.
-
The boolean data type in Java corresponds to
bool in C++.
-
Java allows break/continue
to a labeled outer loop.
-
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).
-
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.
-
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.
-
Java contains standard libraries that help with
solving certain tasks that in C++ require non-standard
third-party libraries.
-
Exception handling is, in general, better designed
and more robust in Java than in C++.
-
Unlike in C++, in Java you cannot arrange for explicit
calls to more that one other constructor in a given constructor.
-
Java insists that boolean operations always be performed
left-to-right, while this is not necessarily true in C or C++.
-
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.
-
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.
-
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
-
Comments using // as well as
comments using /* */
-
The names of most primitive data types
(char, short, int,
long, float, double)
-
All looping and selection constructs
(for, while, do..while,
if..else, switch, ?:)
-
Method overloading in Java and function overloading in C++