C++, Java, and C#
This is a little comparison between Java, C++, and C#.
Java vs. C++
Java was designed after C++. If you come from a C++ background, it may help
to note that, in Java:
- There are no global variables nor global functions
- boolean is NOT considered an integral type (you can't even cast between integers and booleans)
- There's no preprocessor, so no #define, #include, #ifdef, ...
- There no need for "header files" — the class file contains everything a client needs to use the class.
- You cannot write "long int" — you must say "long"
- Uses Unicode, not ASCII, is used
- There are explicit pointers
- Arrays know their size
- There's a much nicer syntax for arrays and array types
- There are operators:
instanceof, ">>>", ">>>="
- There are no operators:
sizeof, &, "*", "->", ".*", ".->"
- Break and continue statements may be labeled
- There's no goto statement
- Forward references are fine and used all the time
- There's no separation of class declaration and definition (use interfaces instead)
- There're no structs, unions, bitfields, typedefs (structs can be simulated;
the others aren't needed)
- There's no special syntax for base class or member construction
- Class fields are automatically initialized to default values
- You can supply initial values of fields at point of field declaration
- Garbage collection of objects is implied
- Classes can be final (meaning you can NOT extend them)
- Synchronization is built into the language
- You don't mark methods "virtual" to get overriding
- There's no multiple inheritance (use interfaces anyway)
- "Templates" are much simpler than those of C++
- There's no operator overloading
- Single argument constructors are automatically "explicit"
- Any object can be a monitor
- String objects are NOT arrays of characters: you cannot use the "[]" operator on them, and you cannot modify them! StringBuffers behave more like the C++ string
- Arrays can be anonymous, e.g.
a = new int[]{10, 7, 3, 1, 5}
- The try statement can have a finally clause
- If code can throw an exception derived from Exception but not RuntimeException, it must be wrapped in a try block that catches the exception, or the exception must be mentioned in the method's throws clause
If you come from a Java environment and have to learn C++, first ask
why C++ instead of C# or plain C. Then reverse the above points.
Java vs. C#
Despite the name, C# is much, much, much closer to Java than to C++.
Both C# and Java are actively evolving, and sometimes you have
to look really closely to tell them apart:
But not everything about these languages is the same:
- C# has real closures; don't expect these in Java until at least Java 7.
Java EE and .NET
The two main platform for industrial-strength enterprise applications
are Java EE (sometimes called just "Java") and .NET.