There are three behavioral differences between an array in C++ and a similar array in Java:
An array in Java is an object (i.e., an instance of an unnamed class), so, like any object, it may have instance fields. Every Java array has a field defined as public int length;
that stores the size of (or the number of elements in) the array.
int[] scores = new int[10]; for (int i = 0; i < scores.length; i++) . . . scores[i] . . .; |
const int size = 10; int scores[size]; for (int i = 0; i < size; i++) . . . scores[i] . . .; |
Java | C++ |
length
, but C++ requires a separate variable.
An array in C++ is a primitive, unstructured data type - it is an address that is incapable of storing additional data. C++ programmers must maintain a separate, distinct variable to track the size of an array.
C++ arrays do NOT have a .length
instance field.
Arrays, in both the C++ and Java programming languages, are zero-indexed, meaning that legal index values range from 0 to size - 1 (where size is the maximum number of elements in the array).
IndexOutOfBoundsException
.
It is easier to get a correct array-based program running in Java than in C++, but Java's bounds checking does have some overhead. While employed in the computing industry, I had access to a FORTRAN compiler with the option of enabling or disabling bounds checking. Using an array-based benchmark called LINPACK, I was able to examine the impact of enabling bounds checking versus disabling the feature. Bounds checking doubled the executable file size and increased the run time by 50%. Most programs are less array-intensive than LINPACK and suffer less performance penalty. Nevertheless, C++ programs will always outperform similar Java programs in this regard.
#include <iostream>
using namespace std;
int main()
{
int array0[10] = {}; // initialize all to 0
int array1[10];
int array2[10] = {}; // initialize all to 0
for (int i = 0; i <= 10; i++) // index out of bounds
array1[i] = 25;
// either array0 or array2 is corrupted
for (int i = 0; i < 10; i++)
cout << "array0: " << array0[i] << endl;
for (int i = 0; i < 10; i++)
cout << "array2: " << array2[i] << endl;
return 0;
}
|
array0: 0 array0: 0 array0: 0 array0: 0 array0: 0 array0: 0 array0: 0 array0: 0 array0: 0 array0: 0 array2: 25 array2: 0 array2: 0 array2: 0 array2: 0 array2: 0 array2: 0 array2: 0 array2: 0 array2: 0 |
(a) | (b) |
Both C++ and Java allow programmers to create arrays of objects, but a Java program requires several steps, resulting in an organization with numerous pointers. (Technically, Java has references, but Java references differ little from C++ pointers.) The following picture illustrates how a Java program creates an array of Employee objects.
new
operator, which returns an address or pointer. Programmers may replace the constant, 5, with a variable. We often program steps (a) and (b) as a single statement.new
operator and its address stored in the array. This technique has the advantage that the program only creates and initializes the objects it needs, saving memory and construction time otherwise wasted building unneeded objects.Where Java provides only one way of creating and organizing an array of objects, C++ provides four. Having multiple options makes C++ very flexible, but the cost is additional, and sometimes complex, notation as the following figure illustrates:
Employee emp[5]; |
![]() |
(a) | |
Employee* emp;
emp = new Employee[5];
|
![]() |
(b) | |
Employee* emp[5]; for (int i = 0; i < 5; i++) emp[i] = new Employee; |
![]() |
(c) | |
Employee** emp; emp = new Employee*[5]; for (int i = 0; i < 5; i++) emp[i] = new Employee; |
![]() |
(d) |