Comparing C++ and Java Many developers already have experience with an object-oriented programming language like C++. As you make the transition to Java, you will encounter many differences, despite some strong similarities. In this excerpt from "
Thinking in Java", Bruce Eckel highlights the important differences that C++ programmers should be aware of. As a C++ programmer, you already have the basic idea of object-oriented programming, and the syntax of Java no doubt looks familiar to you. This makes sense since Java was derived from C++. However, there are a surprising number of differences between C++ and Java. These differences are intended to be significant improvements, and if you understand the differences you''ll see why Java is such a beneficial programming language. This article takes you through the important features that distinguish Java from C++. The biggest potential stumbling block is speed: interpreted Java runs in the range of 20 times slower than C. Nothing prevents the Java language from being compiled and there are just-in-time compilers appearing at this writing that offer significant speed-ups. It is not inconceivable that full native compilers will appear for the more popular platforms, but without those there are classes of problems that will be insoluble with Java because of the speed issue.
Java has both kinds of comments like C++ does.
Everything must be in a class. There are no global functions or global data. If you want the equivalent of globals, make
static methods and
static data within a class. There are no structs or enumerations or unions, only classes.
All method definitions are defined in the body of the class. Thus, in C++ it would look like all the functions are inlined, but they’re not (inlines are noted later).
Class definitions are roughly the same form in Java as in C++, but there’s no closing semicolon. There are no class declarations of the form
class foo, only class definitions. class aType { void aMethod( ) { /* method body */ } } There’s no scope resolution operator
:: in Java. Java uses the dot for everything, but can get away with it since you can define elements only within a class. Even the method definitions must always occur within a class, so there is no need for scope resolution there either. One place where you’ll notice the difference is in the calling of
static methods: you say
ClassName.methodName( );. In addition,
package names are established using the dot, and to perform a kind of C++
#include you use the
import keyword. For example:
import java.awt.*;. (
#include does not directly map to
import, but it has a similar feel to it).
Java, like C++, has primitive types for efficient access. In Java, these are
boolean,
char,
byte,
short,
int,
long,
float, and
double. All the primitive types have specified sizes that are machine independent for portability. (This must have some impact on performance, varying with the machine.) Type-checking and type requirements are much tighter in Java. For example:
.