This is a quick guide to C++ keywords and concepts, and the Java equivalents. I've tried to include everything useful I can think of, but it's probably not complete.
C++ | Java equivalent | |
---|---|---|
Boolean (doesn't exist in C++) | boolean true false |
Java has a real Boolean type, not the C++ hack of using integers, and assigning true/false based on zero versus non-zero. |
break |
As for C++ | In addition to breaking out of the innermost control block (as in C++), you can also specify labels and break to a label. |
case |
As for C++ | |
try |
Similar to C++, plus throws |
Exception handling is similar to C++, but Java is stricter than C++ with exception specifications. Also, |
char |
As for C++, plus boolean and byte |
Unlike in C++, Java numeric types are signed (so there's no
signed and unsigned keywords) and have fixed sizes on all platforms. |
class |
class as in C++; struct and union
don't exist. |
Use classes instead of structs ; use inheritance
instead of unions . |
Concurrent processing | synchronized |
Java has built-in support for concurrent processing using threads and synchronisation. |
const |
Partial support with final |
const has many different meanings in C++. Java doesn't
support all of them. In fact, it doesn't even come
close. |
Constructor | As for C++ | As in C++, a constructor has the same name as the function name, and there can be more than one constructor for a class. |
delete |
None | Java has automatic garbage collection, so
there's no need for delete . |
Derived class | extends |
Example:C++: class Fred : public Base Java: class Fred extends Base |
Destructor | finalize
| Because Java provides automatic garbage collection, you'll probably be writing fewer destructors than in C++. |
if |
As for C++ | |
enum |
None | Java has no support for enums. |
extern |
None | There's no real equivalent to C++'s extern statement,
because Java doesn't require every external
reference in a source file to be declared before use. |
extern "C" |
native |
Java can link to code written in other languages by calling native methods. |
friend |
package |
The Java package mechanism provides the power of friend classes, with much better control. |
Function overloading | As in C++ | You might use function overloading slightly more than in C++, because Java doesn't support default values for function parameters. This can be simulated by providing different overloaded functions. |
goto |
Not used, but a reserved word | |
inline |
None | There is no clear equivalent to inline , though
declaring methods as final might make them inline. As
with C++, it's up to the compiler. |
Multiple inheritance | implements |
Java doesn't have true multiple inheritance. |
new |
As for C++ | This is used more extensively than in C++, because all objects
(i.e. non-primitive types) have to be created using new .
In C++, this is only needed for non-stack-allocated objects.
There is no "placement new" operator. |
NULL |
null |
The Java version is a real keyword, not a #define'd constant. |
operator |
None | Java doesn't support operator overloading. |
Pointers | None | Java pointers are handled internally by the compiler. There is no "pointer notation" nor pointer arithmetic. |
private |
Similar to C++ | These keywords are similar to their use in C++, except that they are applied individually to members rather than grouping them together. The default access is "package", which doesn't have a special keyword. |
Pure virtual (the =0 notation) |
abstract |
Unlike in C++, this is applied to classes as well as member functions. |
References | Implicit way of accessing objects | Java objects are automatically accessed as references - there's no special notation to indicate a reference. |
return |
As for C++, plus finally |
Java allows a finally block, which is executed on any
return or exception return from a function. |
sizeof |
None | This operator isn't available in Java. Since all primitive data types have known sizes, it isn't necessary. |
static |
Similar to C++ use in classes | The C use of static (to restrict access to file scope
or function scope) has no place in Java at all. The C++ use for class
member functions is almost the same in Java. Java adds one extra
feature in a class: You can do class initialisation by including a
block of code with just the static keyword before it. |
template |
None | Java doesn't support templates |
this |
As for C++, plus super |
this and super both refer to the current
object. super refers to members in the base class, in
places where it's important to make a distinction. |
typedef |
None | |
virtual |
None | Java members are virtual by
default. |