Java | C++ | |
---|---|---|
Compiling source code | When we compile a source file a .class file is generated in java byte-codes ( machine independent = portability ) that is not translated into machine language instructions until in the run time when interpreted by the Java Virtual Machine ( machine dependent ). | A source file is compiled and linked to produce an .exe file in machine language that is ready to be executed when the file is loaded into memory and run by the operating system. |
Function definitions | All function definitions and variable declarations are inside Classes even main(). Nothing is allowed to be outside a class except imports and package names. | Function definitions are outside class definitions. They have prototypes whose place decides function scope |
Including classes | Importing a package.* just directs the compiler to the location of classes used in the code so that only these classes are included not the whole package. | while in C++ I think that including a header file means that all classes in that header file will be compiled and linked to the source file. |
Ending class definitions | Class definitions end with } without a semicolon. | Class definitions end with }; |
Creating objects | All objects must be created with new, so they are all dynamically allocated in the heap. Only primitive data types can be allocated on the stack. | Objects can also be created and allocated without new so they are put on the stack in the scope of their declaration. |
Declaring a reference: | Declaring a reference to an object without using new to allocate the object only reserves space for the reference and no object is created in memory until new is used and the resulting object is given to the reference to point to it. | Declaring an object without initializing it makes the compiler call the default constructor for the object's class and memory is allocated for the object in the stack. |
Calling static methods | ClassName.methodName( args.. ); | ClassName::functionName( args.. ); or ObjectName.functionName( args..); |
Wrapper classes | Java has wrapper classes for primitive data types that contains static methods for handling them e.g. String string = Integer.toString( intVariable ); | C++ doesn't normally have them in its standard libraries |
Array declaration and memory allocation: | An array is considered an object that has to be allocated by new.int c[]; declares the array reference c = new int[ 12 ]; allocates the array and automatically initialize its elements to zero for numeric primitive types, false for boolean, null for references ( non-primitive types) int c[] = new int[ 12 ]; you can never specify the number of elements in the [] in a declaration unlike C++. For multiple array declarations: double a[], b[]; Elements of an array of a primitive data type contain values of that data type. ( they have the value null by default ) Allocating and initializing arrays in declarations: int n[] = { 32, 27, 64, 18, 95, Array size is determined by the number of elements in the list. Here new operator is provided by the compiler and the array object is also dynamically allocated. When a Java program is executed. the Java interpreter checks array element subscripts to be sure they are valid. If there is an invalid subscript, Java generates an exception: ArrayIndexOutOfBoundsException. | int c[ 12 ]; declares and allocated memory for 12 int elements without initializing them also memory is not dynamically allocated but instead it is preserved from the start and is allocated in the stack I presume. double a[ 100 ], Elements of an array of any data type contain values or objects of that data type. initializing arrays in declarations: int n[ 10 ] = { 0 }; initialize elements of array n to 0 int n[] = { 1, 2, 3, 4, 5 }; array size determined by the number of elements in the list No checking for array bounds is done by the compiler so the programmer must be very carful not to exceed the array length |
Multiple subscripted arrays | Doesn't directly support multiple-subscripted arrays but allows the programmer to specify single-subscripted arrays whose elements are also single-subscripted arrays, achieving the same effect. int b[][] = { { 1, 2 }, b[ 0 ] is a reference to an array of 2 elements int b[][]; 3 by 3 array allocated int b[][]; allocates rows b[ 0 ] = new int[ 5 ]; allocates columns for row 0 b[ 1 ] = new int[ 3 ]; allocates columns for row 1 | Directly supports multiple-subscripted arrays and their elements are placed consecutively in memory row after row and the elements are actually located by pointer arithmetic. int b[][] = { { 1, 2 }, causes an error in C++ "error: declaration of 'b' as multidimensional array must have bounds for all dimensions except the first." int b[][ 3 ] = { { 1, 2 }, declares and preserves memory for a 2 by 3 multidimensional array b and initializes missing element in the list b[ 0 ][ 2 ] by 0. |
Passing arguments | Java does not allow the programmer to decide whether to pass an argument call-by-value or call-by-reference: | You can pass primitive data types or objects either call-by-value or call-by-reference ( using pointers or references to them ). |
Returns | When returning information from a method via a return statement: Primitive data type variables are always returned by value (a copy is returned). | Objects and primitive data type variables can be returned by value or by reference |
Constant variables | Constant variables ( read only variables ) are declared using final. | They are declared using const. |
Thursday, June 28, 2007
Java vs. C++ - Part One
These are only some of the differences between Java and C++ that I found so far that drew my attention and I think are worth mentioning.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment