Comparing C, C++, and Java
C, C++, and Java differ in language features and execution models.
| Feature | C | C++ | Java |
|---|---|---|---|
typedef |
O | O | X |
#define |
O | O | X |
goto |
O | O | Reserved but unavailable |
| Structures | O | O | X |
| Unions | O | O | X |
| Manual memory management | O | O | X |
| Operator overloading | X | O | X |
| Function or method overloading | X | O | O |
| Multiple class inheritance | X | O | X |
| Platform-independent bytecode | X | X | O |
Main Function
C and C++ programs begin at a main function. Java applications use a main method in a class.
C++
#include <iostream>
int main(int argc, const char *argv[]) {
std::cout << "Hello, World!\n";
return 0;
}
Java
public class Main {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
In C and C++, main returns int and may omit its parameters. Java uses public static void main(String[] args).
Define Macros
C and C++ use a preprocessing step. A #define macro replaces tokens before compilation.
#define HELLO "Hello, World!\n"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Java has no equivalent preprocessor directive. Constants are commonly declared with static final.
public static final String HELLO = "Hello, World!\n";
Prefer language features such as constants, enums, and inline functions over macros where possible. Excessive macro use can make debugging difficult.