next up previous index
Next: Initialisation of Both Together Up: Array Variables Previous: Array Bounds   Index

Initialisation of Array Elements

When Java arrays are created by allocation using new, their elements are automatically initialised to 0 for numeric types, to the null character for char arrays, to false for boolean arrays and to the null reference for object arrays. With the possible exception of null for object arrays, it is generally better not to rely on implicit initialisation. Explicit initialisation makes code more secure and more intelligible. This can be done by
for (int i = 0; i < 20; i++) {
    a[i] = 0;
}
for example.



Peter Williams 2005-06-07