Java Reference
In-Depth Information
boolean [ ] prime; // array of booleans
double [ ] coordinates; // arrays of reals formatted using double precision
Similarly, static class arrays are declared inside the body of a class using the
keyword
static
:
class Example{
static int[]x;
static boolean [ ] prime;
...
}
These (class) static array variables can then be used and shared by
any
function
of the class. In both local/class cases, arrays are allocated into the
global
memory, and not into the function call stack. Only the references of arrays
may be stored into the function call stack for locally declared arrays.
4.2.2 Creating and initializing arrays
Arrays are
created
and
initialized
by default using the Java reserved keyword
new
:
int[]x=newint[5];
boolean [ ] prime = new boolean[16];
The size of arrays needs to be specified:
x=new int [32];
The size of an array can also be given as an integer arithmetic expression like
2*n
,etc:
x=new int [2*n+3];
Arrays can only be declared once but may eventually be created and initialized
several times. This recreation process overrides the former creation/initializa-
tion:
x=new int [2*n+3];
...
x=new int [4*n-2]; // overrides the former creation and initialization
By default, initialization of arrays is performed by Java by filling all its elements
with 0, or by casting this 0 to the equivalent array element type: For example,
false
for booleans,
0.0d
for
double
,
0.0f
for
float
, etc. Initialization can
also be explicitly done by
enumerating
all its elements separated by comas “,”
within curly brackets
{}
as follows:
int [ ] prime={2, 3, 5, 7, 11, 13, 17, 19};
boolean prime[]={ false, true, true, true, false, true, false, true};
Search WWH ::
Custom Search