How the Java Language Differs from C and C++ |
The Java language does not support pointers,struct
, orunion
.pointers
The Java language passes all arrays and objects by reference but does not have an explicit pointer type. You cannot construct a reference to anonymous memory. In addition to making programming easier, this prevents common errors due to pointer mismanagement.struct and union
The Java language does not support eitherstruct
orunion
. Instead use classes or interfaces to build composite types. For example, in C, you might declare a structure that contains information about employees like this:The last line of this structure is a pointer to a function. When you allocate and initialize an employee structure, you must supply the structure with a pointer to a function, and that function must be defined as indicated in the struct declaration. In the example above, thestruct employee { char name[NAMESIZE]; char address[ADRSIZE]; long ssn; double salary; double (*compute_raise)(double, double); };compute_raise
function must accept two doubles are arguments, and return a double. This function will do the trick.Note that even thoughdouble compute_raise(double salary, double percent) { return salary * percent; }salary
is part of the structure we still need to supply that value to the function. Here's a main program that creates, initializes an employee structure, and then computes a 10% raise for that person. Note that the main program has access to information (the employee's salary) that ideally should be kept private.main() { struct employee George = { "George", "NOWHERE", 123456789, 45000.00, compute_raise }; printf("raise = %f\n", George.compute_raise(George.salary, 0.10)); }In the Java language, the need for structures is completely obsoleted by classes. which provide for a cleaner way to bundle data and methods together, and a way to keep some of that data private to the class.
In the Java language, instead of the struct declared above, you would declare a class to maintain information about employees:
Note that a complete method declaration forclass Employee { String name; String address; long ssn; private double salary; double compute_raise(double percent) { return percent * salary; } Employee(String a_name, String a_address, long a_ssn, double a_salary){ name = a_name; address = a_address; ssn = a_ssn; salary = a_salary; }; }compute_raise
including the method body is implemented within the class. Also note that the salary instance variable is declared private--this means that only an instance of this class has access to the salary information-- thereby keeping the information protected from prying eyes. Try doing that with C. A constructor is included for convenience.Notice how this application can compute George's raise without ever obtaining the salary information directly.
class MainClass { public static void main(String args[]) { Employee george = new Employee("George", "NOWHERE", 123456789, 45000.0); System.out.println("raise = " + george.compute_raise(0.10)); } }
How the Java Language Differs from C and C++ |