Java`s syntax is largely based on C/C++ with some notable differences

advertisement
javaBasics 1/3
Java's syntax is largely based on C/C++ with some notable differences.
Comparison with C/C++:
1.
No preprocessor. There are no #ifdef, #define, or #include
statements. No macros. No conditional compilation.
2.
No global variables. All variables are defined relative to the
classes that contain them. A clean "namespace".
3.
Uniform size of primitive types. short, int, float, and long types
are always the same size on all Java implementations, regardless of
machine architecture and operating system.
For example, integer sizes are:
byte -- signed, always 8 bits
short -- signed, always 16 bits
int -- signed, always 32 bits
long -- signed, always 64 bits
4.
No goto statement.
5.
No struct and union types. A class encapsulates all the information
that one would put in a struct or union. Inner classes provide
similar functionality for organizing related variables.
6.
No enum.
7.
No header files. Java does not require you to define a method
before it is invoked. So there is no need for header files.
8.
No bitfields.
9.
No typedef keyword.
Fixed sets of named values cannot be done conveniently.
Need to use method calls to pack or unpack bits.
Can't create aliases for type names.
10. No variable argument lists.
javaBasics 2/3
Comparison with C/C++ references, pointers, and memory:
1.
No pointers. There are no pointer declarations in Java.
Hence no address-of operator (&), no dereference operators (*, ->),
and no sizeof operator. Java has just references, and just the
. address operator. All addresses for references come from
the memory heap.
2.
In C++, after the initialization of a reference, a reference by
itself always stands for the object it points to; therefore, the
value of the reference variable itself cannot be changed.
In Java, a reference by itself always stands for a variable which
contains the address of the object it points to, and may be changed.
Java references have many of the characteristics of C/C++ pointers.
3.
Dynamic memory allocation. The new operator is used to allocate all
non-primitive objects; there are no non-primitive objects that are
statically allocated (as there are in C/C++).
4.
Memory deallocation -- Garbage collection. Memory deallocation
occurs automatically (no free or delete calls). The programmer
creates new instances of classes, "objects", and when the objects
are no longer used, the JVM releases the memory associated with the
objects. This "garbage collection" eliminates a major source of
bugs (memory leaks) in C programs.
5.
Language defined parameter passing. Primitive objects (the numeric
types and boolean) may not be referenced indirectly (always pass by
value). Non-primitive objects (e.g., arrays and class objects) must
be referenced indirectly (always pass by reference).
6.
No pointers to functions. Can't store the address of a method in a
variable, and pass it around. Similar functionality is provided
through interfaces, and "reflection" methods (where one can look up
the methods in a class and invoke them).
javaBasics 3/3
7.
An array declaration (actually, a declaration of an array referenc):
int table[];
or
int[] table;
does not allocate memory for the array.
table = new int[256];
allocates the memory.
8.
C/C++ has arrays of arrays, and arrays of pointers to arrays.
Java has just arrays of references (its pointers) to arrays.
C++ does not allow arrays of references.
E.g. In Java:
int table[][];
or
int[] table[];
or
int[][] table;
is a reference (pointer) to an array of references (pointers) to an
array of integers.
table = new int[10][];
allocates memory for the array of references; each such reference
must then be used to allocate an array of integers before use. E.g.:
table[1] = new int[20];
table[2] = new int[15];
9.
In C/C++, the number of elements of an array that was statically
allocated, or allocated on the stack, could be obtained using
sizeof. In Java, the number of elements of an array is given by
arrayName.length
10. Arrays may be initialized when they are allocated:
int[] table;
table = new int[] { 3, 1, 4, 5, 2 };
is equivalent to:
int[] table = new int[5];
table[0] = 3;
table[1] = 1;
table[2] = 4;
table[3] = 5;
table[4] = 2;
Similarly, for arrays of arrays:
int[][] table;
table = new int[][] { { 1 },
{ 2, 3},
{ 4, 5, 6} };
11. Because arrays are references, parameters are pass by reference.
Download