This appendix contains a description of most of the major differences between C, C++, and the Java language. If you are a programmer familiar with either C or C++, you may want to review this appendix to catch some of the common mistakes and
assumptions programmers make when using Java.
Java does not have an explicit pointer type. Instead of pointers, all references to objectsincluding variable assignments, arguments passed into methods, and array elementsare accomplished by using implicit references. References and
pointers are essentially the same thing except that you can't do pointer arithmetic on references (nor do you need to).
References also allow you to easily create structures in Java such as linked lists without explicit pointers; merely create a linked list node with variables that point to the next and the previous node. Then, to insert items in the list, assign those
variables to other node objects.
Arrays in Java are first class objects, and references to arrays and their contents are accomplished through explicit references rather than pointer arithmetic. Array boundaries are strictly enforced; attempting to read past the ends of an array
produces a compile or runtime error. As with other objects, passing an array to a method passes the original reference to that array, so changing the contents of that array reference changes the original array object.
Arrays of objects are arrays of references that are not automatically initialized to contain actual objects. Using the following Java code produces an array of type MyObject with ten elements, but that array initially contains only nulls:
MyObject arrayofobjs[] = new MyObject[10];
You must add actual MyObject objects to that array:
for (int i; i< arrayofobjs.length. i++) { arrayofobjs[i] = new MyObject();
Java does not support multidimensional arrays as does C and C++. In Java, you must create arrays that contain other arrays.
Strings in C and C++ are arrays of characters, terminated by a null character (\0). To operate on and manage strings, you treat them as you would any other arrays, with all the inherent difficulties of keeping track of pointer arithmetic and taking
care not to stray off the end of the array.
Strings in Java are objects, and all methods that operate on strings can treat the string as a complete entity. Strings are not terminated by a null, nor can you accidentally overstep the end of a string. (As with arrays, string boundaries are strictly
enforced.)
All memory management in Java is automatic; memory is allocated automatically when an object is created, and a runtime garbage collector (the "gc") frees that memory when the object is no longer in use. C's malloc and free functions do not
exist in Java.
To "force" an object to be freed, remove all references to that object (assign all variables and array elements holding it to null). The next time the Java gc runs, that object is reclaimed.
All Java primitive data types (char, int, long, and so on) have consistent sizes and behavior across platforms and operating systems. Java has no unsigned data types as does C and C++ (except for char, which is a 16-bit unsigned integer).
The boolean primitive data type can have two values: true or false. Boolean is not an integer, nor can it be treated as one, although you can cast 0 or 1 (integers) to boolean types in Java.
Composite data types are accomplished in Java exclusively through the use of class definitions. The struct, union, and typedef keywords have all been removed in favor of classes.
Casting between data types is much more controlled in Java; automatic casting occurs only when there is no loss of information. All other casts must be explicit. The primitive data types (int, float, long, char, boolean, and so on) cannot be cast to
objects or vice versa; methods and special "wrapper" classes convert values between objects and primitive types.
Operator precedence and association behaves as it does in C. Note, however, that the new keyword (for creating a new object) binds tighter than dot notation (.), which is different behavior from C++. In particular, note the following expression:
new foo().bar;
This expression operates as if it were written like this:
(new foo()).bar;
You cannot overload operators in Java as you can in C++ . The , operator of C has been deleted.
The >>> operator produces an unsigned logical right shift. (Remember, there are no unsigned data types.)
You can use the + operator to concatenate strings.
Although the if, while, for, and do statements in Java are syntactically the same as they are in C and C++, there is one significant difference. The test expression for each control flow construct must return an actual boolean value (true or false). In
C and C++, the expression can return an integer.
The goto keyword does not exist in Java (it's a reserved word but currently unimplemented). You can, however, use labeled breaks and continues to break out of and continue executing complex switch or loop constructs.
Java does not support mechanisms for variable-length argument lists to functions as does C and C++. All method definitions must have a specific number of arguments. In addition, you cannot use void in an empty argument list.
Command-line arguments in Java behave differently from those in C and C++. The first element in the argument vector (argv[0]) in C and C++ is the name of the program itself; in Java, that first argument is the first of the additional arguments. In
other words, in Java, argv[0] is argv[1] in C and C++; there is no way to get hold of the actual name of the Java program.
The following other minor differences from C and C++ exist in Java: