|
|
|
|
|---|---|---|---|
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. | wchar_t.A bool type is available.
int, short,
long, byte,
float, double,
boolean, charThe void type may be used as a return type.The sizes of numeric types are platform-independent. All char values are in Unicode | ||
#define TRUE 1 |
bool type exists.
A bool may be converted to or from an int;
a pointer may be coverted to a bool |
boolean type exists. No conversion between
booleans and ints is possible |
|
ints | ints
in some contexts | Enumeration interface is not the same thing. | |
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. |
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 |
|
structs;
name with typedef for convenience |
structs are also permitted. In neither case
is a separate naming step needed |
||
char*;
in the latter case storage needs to be allocated
separately (e.g., with malloc).Functions manipulating such strings are found in <string.h> |
string class is available from
<string> in the standard library
(not from <string.h>).C-style strings are also permitted. |
String class is available in the standard
java.lang package.Strings are not mutable; StringBuffers are. |
|
vectors and strings
(errors are handled by throwing exceptions) |
Vectors, and Strings. |
||
int could be implicit. |
|||
for loop control variables |
Their scope then extends to the end of the loop (formerly, it was to the end of the block enclosing 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 * const.
It is particularly helpful to constrain parameters (including implicit
parameters of member functions) and return types in this way,
when appropriate. |
public static final |
|
(double) ifor an integer i. Many are performed automatically; for example double d=6; |
static_cast, reinterpret_cast,
or const_cast. Also available are
dynamic_casts and C-style casts. |
||
Referencing necessary in I/O |
Referencing seldom necessary |
||
Arrays passed as pointers. Call by reference may be simulated by passing pointers |
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. |
-> cannot be used (the dot syntax leads to
an automatic dereference). |
|
void keyword.Empty parentheses mean an arbitrary number of arguments. |
|||
Function prototypes may be used to declare without defining. |
Function prototypes may be used as in C. |
||
+ operator is provided both for Strings and for numeric types). |
|||
main function (or method) |
Has return type int; normally returns 0. |
public static void.The Java interpreter takes a class as argument and executes the class's main method. |
|
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. |
String[]
for main.Elements 0 to k correspond to command-line arguments 1 to k+1. The command itself is not represented |
||
<stdio.h> |
<iostream><fstream> |
java.io packages. Custom packages
can provide more functionality |
|
FILE * |
istream, ostream, ifstream,
and ofstream |
java.io package,
InputStream, PrintStream, and many others |
|
FILE*s stdin, stdout, and stderr
may be passed to the file I/O functions. |
cin, cout, and cerr |
InputStream System.in and the PrintStreams System.out and System.err |
|
printf, fprintf, scanf, fscanf |
<< and >> operators.Also put, get, getline,
read, and write |
java.io package provides read,
print, and other methods |
|
\n |
endl to output stream |
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") |
|
<iomanip>
|
java.text package provides several classes to support formatting.
|
||
fopen and fclose, which return 0 on failure |
open or close, or use the constructors with file name arguments to open. |
close to close |
|
/* and */
(although C++ comments are becoming standard) |
//
and extend to end of line |
/** and
*/ can be used for
automatic documentation generation. |
|
signal.h |
catch and
throw needn't be named. |
Exception.
Methods must list any exceptions they might throw. Arguments
to catch and throw must be named.An Error class is also available |
|
Use malloc to allocate from "heap", then cast to
appropriate type; malloc returns NULL on failureUse 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. |
new required.Garbage collection used to free storage. |
.h extensions are used
for defining public types and constants,
and declaring global variables and public functions;they may be #included by other files |
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).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 . or an import statement
must be used, e.g.,import java.io.*Single classes may also be imported. Object |