Some Differences between Java and C++ Huaqing Wang 1. Java and C++ Program Structures Java Program consists of C++ Program consists of Packages namespace Variables, constants, unions, classes, structures, enum and functions Java packages consists of C++ namespace contains packages enitites listed above. classes namespace syntax: interfaces namespace xyz { enum entities } Java package syntax: package name ; entities listed above .... ------- end of file. One file contains one or more classes/interfaces. Samce package name can be used in different files. A class/interfaces can nested classes and/or interfaces. An nested/inner class/inferace may be a class member or may not a class member depending on where the innder class is declared. 2. Data Types * Primitive Data Types Java C++ ============ ================================= 8 primitive fixed-length More primitive data types due to unsigned and unsigned. The lengths of primititive data types are not fiexed at language design time, but at language implementation time. Fixed length for each Lengths are implementation dependent primitive data type on every platform. char: Unicode 2 bytes Ascii, 1 byte. No unsigned int, char, long etc. in Java. boolean * User defined types: Java C++ ===================== ====================================== class class interface struct enum enum union 3. Statements Java and C++ have almost identical syntax as for assignment, control structures ( if and switch statements), and repetitive structures (for, while, do-while statements). However, C++ allows integer expressions appear where a logic expression is needed while Java allows only boolean expression anywhere a logical expression is required. Logical Expressions In control statement such as if ( condition ) ... ; while( conditin ) ...; for( ; condition ; ) ... ; integer expresions can be used in those statements. However, you can not integer expression when boolean expression is requred in Java. The boolean expression must be used. while ( exp ) ... // illegal in Java if exp return an integer. Therefore, the logic connectives such as ||, &&, !, ?: must work on boolean expresions, not integer expression allowed when a logica expression is required in Java. 4. Reference and Pointer Types Pointer type : C/C++ have a pointer type variable and C++ has reference type. Reference type: Java has no pointer type, and it has reference type. I would like to say Java has only pointer type for non-primitive type. 4. Arrays * Array in Java and C++: In C++, the array variable declaration will result element allocation. In Java, the array variable is a reference variable, and the elements are allocated through the new operator. There is length field in each array of java that contains the number of elements in one-Dimensional arrays or rows in two-dimensional arrays. a[i].length returns the number of elements of ith row. int ia[10]; // Legal in C++, illegal in Java. int a[], b ; // Legal in Java, illegal in C++. int [] a, b ; // Both a and b are 1-D arrays in Java; illegal in C++. double [][] darr = new double[10][10]; // legal in Java; illegal in C++ int ia[] = {1, 2, 3, 4, 5} ; // Legal in both C++ and Java. 5. Class Member access control qualifiers: C++ Java: ----------------------------- --------------------------- 1. private, public, protected same as C++ plus package-wide accessibility 2. default--> private default->new qualifier for package scope. 3. Specify access control Specify access control on each line. groups. 4. Instance varaible can be initialized. 5. Inline function no correspondance. 6. Ini static member var. Ini. static variable where defined. outside class. 7. Access static member :: Class.static_member. 8. Constrcutor Same systam and same functionality. 9. Destructor. protected void finalize(); less usage. 10. class A{virtual f()=0.. abstract class A { ... } functions are automatially virtual if necessary. 11. abstract f(); // must be overloaded. 12. inheritance class A: B class A extends B { ... } 13. multiple inheritance. class A extends B implements C, D {...} 14. No class-level access class can be public, private, protected or control. package-wide accessible. 6. Other Additions in Java 1). Large number of packages and classes that make it possible to write programs with features such as web-based, networking, multi-threading, remote procedure call, graphics, messaging and many other things, WITHOUT purchaing additional libraries. 2). Command line arguments: command appeared as the first argument in C++, while command does not appear in the String arg[] in Java. The number of parameters can be retrieved by arg.length in Java while the nnumber of arguments at command line is passed as the first arguement of main(int argc, char** arg). 3). instanceof operator to get run-time-type-identification. 4). Interface definitions. 5). Packages are used to divide classes and/interfaces into groups. 6). The command/operator, break, allows you to break to different location. 7. Features omitted 1). No template, friend function, name spaces, default parameters, nor operator overloading. 2). No preprocessors in Java 3). No multiple inheritance in some sense.