|primitive atomic underived types
unsigned are available.
wchar_t is available via
for wide characters.
void type is available, but may not have instances.
|The primitive types of C are all available, including |
bool type is available.
void type may be used as a return type.
The sizes of numeric types are platform-independent.
char values are in Unicode
||No primitive type available; may be simulated by
#define TRUE 1
#define FALSE 0
typedef int boolean;
bool type exists.
bool may be converted to or from an
a pointer may be coverted to a
boolean type exists. No conversion between
ints is possible
||Available. Enumeration values may be coerced to |
|Available. Enumeration values cannot be coerced to |
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
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.
new the sizes when initializing.
Arrays may be assigned but not compared.
Vectors, which may have variable size,
are available in the
|other user-defined structured types
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).
||Declared as character arrays or with
in the latter case storage needs to be allocated
separately (e.g., with
Functions manipulating such strings are found in
string class is available from
<string> in the standard library
C-style strings are also permitted.
String class is available in the standard
Strings are not mutable;
||Not provided by the language for arrays.
(errors are handled by throwing exceptions)
|Handled by exceptions for arrays,
||Must precede statements in a compound statement.
In older C, declarations as
int could be implicit.
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.
const. Doing so for arrays makes them immutable
const. Distinguish constant pointers
(as opposed to pointers to constants) by using
It is particularly helpful to constrain parameters (including implicit
parameters of member functions) and return types in this way,
public static final
|coercion and casts
||A wide range of type conversions is possible;
for an integer i. Many
are performed automatically; for example
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
||Call by value.
Arrays passed as pointers.
Call by reference may be simulated by
|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
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++,
-> cannot be used (the dot syntax leads to
an automatic dereference).
Empty parentheses mean an arbitrary
number of arguments.
|Use empty parentheses.
||Use empty parentheses.
||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.
||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).
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
||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
Elements 0 to k correspond
to command-line arguments 1 to k+1.
The command itself is not represented
|source of I/O functions
java.io packages. Custom packages
can provide more functionality
|types for I/O
PrintStream, and many others
|names for standard input and output
||Separate I/O functions exist for standard I/O and file I/O,
may be passed to the file I/O functions.
System.in and the
|typical I/O functions
java.io package provides
print, and other methods
||Output the character
endl to output stream
java.io package provides a
println function for
It may be invoked with no argument.
A platform-independent newline String may be obtained as the value of
||Formatting string passed to I/O function
||Special manipulators that may be added to streams
are available in the standard library
java.text package provides several classes to support formatting.
|opening & closing files
fclose, which return 0 on failure
close, or use the constructors with file name arguments to open.
|Use an appropriate constructor to open; use
close to close
(although C++ comments are becoming standard)
|As C, but also may begin with
and extend to end of line
|As C++, but the delimiters
*/ can be used for
automatic documentation generation.
||Not explictly available. Implementation-defined handling
may be modified by using
|Exceptions are types (or classes). Methods needn't list
exceptions they throw. Arguments to
throw needn't be named.
|Exceptions are classes, inheriting (perhaps indirectly)
Methods must list any exceptions they might throw. Arguments
throw must be named.
Error class is also available
||Automatic allocation from stack.
malloc to allocate from "heap", then cast to
NULL on failure
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;
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
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
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
|namespaces and packages
||Namespaces are available. Libraries may put their
members into namespaces (e.g.,
the standard library). Namespace members used
must be prefixed with the namespace name followed
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
must be used, e.g.,
Single classes may also be imported.
||Not available. Generic programming is possible
(although not completely safe) due the existence
of the universal superclass
||Both single and multiple inheritance
||Only single inheritance is available.
Interfaces may be used to get some of the
benefits of multiple inheritance