C vs C++ vs Java





Data Types
primitive atomic underived types
int, char, float, double. Prefixes const, long, short, signed, and unsigned are available. A wchar_t is available via <stddef.h> for wide characters. A void type is available, but may not have instances.

The primitive types of C are all available, including wchar_t.
A bool type is available.
int, short, long, byte, float, double, boolean, char
The void type may be used as a return type.
The sizes of numeric types are platform-independent.
All char values are in Unicode
No primitive type available; may be simulated by
#define TRUE 1
#define FALSE 0
typedef int boolean;
A bool type exists. A bool may be converted to or from an int; a pointer may be coverted to a bool
A boolean type exists. No conversion between booleans and ints is possible
enumeration types
Available. Enumeration values may be coerced to ints
Available. Enumeration values cannot be coerced to ints in some contexts
Not available. The Enumeration interface is not the same thing.
user-defined homogeneous structured types
Declare arrays with sizes for each dimension (except perhaps for the size of the first).
An array name may be used as a pointer to its first member.
vectors are available via <vector>. They may have variable size, and may be assigned and compared.
C-style arrays are also available.
Array declarations need the number of dimensions but not their sizes.
Give new the sizes when initializing.
Arrays may be assigned but not compared.
Vectors, which may have variable size, are available in the java.util package
other user-defined structured types
Define as structs; name with typedef for convenience
Define as classes; structs are also permitted. In neither case is a separate naming step needed
Define as classes; no separate naming step is needed
pointer and reference types
Pointer types are available
Pointer and reference types are available. Reference types may be used for formal parameters and return types.
Neither pointer nor reference types are available. Names of objects as well as arrays are treated implicitly as pointers.
recursively defined types
Pointers to the struct being defined may be used in the definition; the struct itself may not.
Pointers to the class or struct being defined may be used in the definition; the class or struct itself may not.
No problems (since instance variables of the class being defined are treated implicitly as pointers).
character strings
Declared as character arrays or with char*; in the latter case storage needs to be allocated separately (e.g., with malloc).
Functions manipulating such strings are found in <string.h>
A string class is available from <string> in the standard library (not from <string.h>).
C-style strings are also permitted.
A String class is available in the standard java.lang package.
Strings are not mutable; StringBuffers are.
range checking
Not provided by the language for arrays.
Available for vectors and strings (errors are handled by throwing exceptions)
Handled by exceptions for arrays, Vectors, and Strings.
Must precede statements in a compound statement. In older C, declarations as int could be implicit.
Are statements
Are statements
for loop control variables
Must be declared outside the loop
May be declared inside the loop.
Their scope then extends to the end of the loop (formerly, it was to the end of the block enclosing the loop).
May be declared inside the loop.
Their scope then extends only to the end of the loop.
Declare as const. Doing so for arrays makes them immutable
Declare as const. Distinguish constant pointers (as opposed to pointers to constants) by using * const. It is particularly helpful to constrain parameters (including implicit parameters of member functions) and return types in this way, when appropriate.
Declare as
public static final
coercion and casts
A wide range of type conversions is possible; e.g.
(double) i
for an integer i. Many are performed automatically; for example
double d=6;
Use static_cast, reinterpret_cast, or const_cast. Also available are dynamic_casts and C-style casts.
C-style cast syntax used. Illegal dynamic casts not caught by the compiler throw exceptions.
referencing and dereferencing
Both operations available.
Referencing necessary in I/O
Both operations available
Referencing seldom necessary
Irrelevant, since pointer variables are unavailable
parameter passing
Call by value.
Arrays passed as pointers.
Call by reference may be simulated by passing pointers
Call by reference available; use reference types for the formal parameters.
Arrays treated as in C.
The call of a member function of a class has a special syntax with . or ->; the object of the class appears to the left of this symbol rather than in the parameter list.
Call by value. Arrays and objects passed as pointers. Messages are passed as in C++, except that -> cannot be used (the dot syntax leads to an automatic dereference).
nullary functions
Use the void keyword.
Empty parentheses mean an arbitrary number of arguments.
Use empty parentheses.
Use empty parentheses.
forward reference
A function should be declared before it is called.
Function prototypes may be used to declare without defining.
A function must be declared before it can be called.
Function prototypes may be used as in C.
A function may call another function even if the second function is defined after the first.
Not permitted.
OK for both functions and operators
OK for functions, not for operators (although a primitive + operator is provided both for Strings and for numeric types).
The main function (or method)
A linked program must have one and only one such function.
Has return type int; normally returns 0.
As in C.
Each class may have one; it may be called by other classes. It must be public static void.
The Java interpreter takes a class as argument and executes the class's main method.
command-line arguments
Use optional formal parameters:
main(int argc, char* argv[])
The first gives the number of command line arguments, and the second contains the command in position 0, and the sequence of command line parameters beginning in position 1.
As in C.
Use a formal parameter of class String[] for main.
Elements 0 to k correspond to command-line arguments 1 to k+1.
The command itself is not represented
source of I/O functions
the java.io packages. Custom packages can provide more functionality
types for I/O
istream, ostream, ifstream, and ofstream
In the java.io package, InputStream, PrintStream, and many others
names for standard input and output
Separate I/O functions exist for standard I/O and file I/O, although the FILE*s stdin, stdout, and stderr may be passed to the file I/O functions.
The streams cin, cout, and cerr
The InputStream System.in and the PrintStreams System.out and System.err
typical I/O functions
printf, fprintf, scanf, fscanf
The << and >> operators.
Also put, get, getline, read, and write
The java.io package provides read, print, and other methods
new line
Output the character \n
Add endl to output stream
The java.io package provides a println function for PrintStreams. It may be invoked with no argument.
A platform-independent newline String may be obtained as the value of System.getProperty("line.separator")
formatting I/O
Formatting string passed to I/O function
Special manipulators that may be added to streams are available in the standard library <iomanip>
The java.text package provides several classes to support formatting.
opening & closing files
Use fopen and fclose, which return 0 on failure
Pass messages open or close, or use the constructors with file name arguments to open.
Use an appropriate constructor to open; use close to close
Other Issues
Delimited by /* and */ (although C++ comments are becoming standard)
As C, but also may begin with // and extend to end of line
As C++, but the delimiters /** and */ can be used for automatic documentation generation.
Not explictly available. Implementation-defined handling may be modified by using signal.h
Exceptions are types (or classes). Methods needn't list exceptions they throw. Arguments to catch and throw needn't be named.
Exceptions are classes, inheriting (perhaps indirectly) from Exception. Methods must list any exceptions they might throw. Arguments to catch and throw must be named.
An Error class is also available
memory management
Automatic allocation from stack.
Use malloc to allocate from "heap", then cast to appropriate type; malloc returns NULL on failure
Use free to deallocate.
new allocates space for objects, given appropriate constructor for class, throws exception on failure.
In older C++, it returned 0 on failure.
delete deallocates by calling appropriate destructor.
C functions also available.
No allocation for objects from stack; new required.
Garbage collection used to free storage.
conventions for separate compilation
Header files with .h extensions are used for defining public types and constants, and declaring global variables and public functions;
they may be #included by other files
Class specifications are in *.h header files; names of standard libraries now have no extension.
Implementations are in a *.cpp file, which includes the corresponding header file (and any others needed)
Member functions defined outside the class are prefixed with the class name followed by ::
Namespaces may be used.

A class definition appears in a file with the class name and a .java extension; no separate class specification is needed.
A class needn't be explicitly included by other classes, but is loaded as needed, provided that its location is known (e.g., from CLASSPATH).
namespaces and packages
Not available
Namespaces are available. Libraries may put their members into namespaces (e.g., std for the standard library). Namespace members used must be prefixed with the namespace name followed by a :: or a using directive or declaration must be used, e.g.,
using namespace std
Packages are available. Libraries may put their classes into packages. Package members used must be prefixed with the package name followed by a . or an import statement must be used, e.g.,
import java.io.*
Single classes may also be imported.
Not available
Not available. Generic programming is possible (although not completely safe) due the existence of the universal superclass Object
Not available
Both single and multiple inheritance are available
Only single inheritance is available. Interfaces may be used to get some of the benefits of multiple inheritance