0:25
When I want to manipulate data in a program, we know that I must
do it through a variable. For example, if my program must
use data of double type, I'll declare a variable of double
type and I'll give it a name. It's exactly the same with an array,
I must declare a variable of array type. Here I must indicate the type
of the elements contained in the array and the fact that it's an array is
indicated by this pair of brackets that precedes the variable's declaration.
So here we once again have a variable name and a type that corresponds
to the fixed-size array of integers type. In Java, there exists an alternative syntax.
So the instruction line we just saw can also be written in this manner
in Java. Just like this instruction line corresponds to the declaration of
a variable of double type, the instruction lines you saw here correspond
to the declaration of a variable "score" of fixed-size array of integers type.
We'll naturally ask ourselves how we can initialize
an array declared this way.
1:30
Two methods are usually used to put values
in an array. I can put the values in an array when
I declare it, that's the first possible solution. In the same manner as
when I declare a variable of double type, I can affect it an
initial value, so that corresponds to a declaration-initialization. I can do
the same for an array, assuming
that I initially know what elements I'm putting into it, and I use a particular syntax:
these curly braces that we'll see in more detail
later. So this corresponds to a declaration-initializazion
of an array that corresponds to the situation 1. Second situation,
exactly the same thing as for a double, when I declare it
and then assign it a value. I can do the same for an array.
So I can simply declare it, and fill it element by element
in the instruction lines that follow.
2:33
So, as we just saw, if I initially know what elements
I'll put in my array, when I declare it: I can simply write a
unique declaration-initialization instruction line to
declare an array and put values in it. Concretely, here
are the steps to observe. I must declare an array, which I do
like this. Then, I must indicate the elements
to put in the array, between curly braces. And then I must put elements
in this array, and every element will be separated
by a comma. So here are the different syntactic rules that I
must follow to declare-initialize a fixed-size array in Java.
3:24
Let's see what memory situation this corresponds to.
Before studying in detail the notion of fixed-size arrays in Java,
and as we are beginning to manipulate advanced types of data, it is
important to understand that in Java, the basic types of data and the
composed, advanced types of data, aren't stored the same way
in memory. For example, if I declare a variable "x" of elementary
type, you must know that when I manipulate a variable of a basic type,
well, that variable will directly store that value. What we can observe
in this little diagram, the variable "x" directly stores the value associated
with it. This isn't the case when I begin manipulating data
of advanced types.
Data of advanced types, like arrays or strings of
characters, which we'll see in future episodes, are in fact
stored by using references, indirections, addresses.
For example, if I declare a variable of type "string of characters", we'll
see that there exists a type for that, called String, so the memory
situation is the following: the variable "v" doesn't directly store the associated
sequence of characters value, but it stores a reference, an address, to the
sequence of characters in question. And this has a strong incidence on
the semantic of assignments in Java. Imagine, for example, that I assign
a variable "v2" to a variable "v1", what does this concretely
mean? Am I modifying the stored reference in "v1"?
Or am I modifying the object referenced by "v1"?
In the same way, the "==" operator's semantic is also
impacted. What does comparing "v1" and "v2" mean? Am I comparing
references? Am I comparing the pointed sequences of characters?
We'll see that there also exists an incidence on the printing.
For example, if I print an object of composed type. What am I trying
to print? An address? Or the [content of the] referenced object? So all these questions
will arise when I manipulate composed type objects in a
Java program.
In Java, an array is an advanced type data, it is therefore manipulated
through an indirection, a reference. So if I declare-initialize
for example a variable "score" of fixed-size array of
integers type in this manner here, the memory situation will be the following:
the variable "score" doesn't directly contain the array's values,
it contains an indirection, a reference, an address to the array.
We suppose that the array's address is here. In the variable "score"
I only store the reference to this array. In jargon terms,
we'll say that the variable "score" "references" or "points to" an array of int.
6:22
We just saw how it is possible to declare-initialize a fixed-size
array in a single instruction line, in Java. This presupposes that we initially
know what values to put in the array. In the most general case,
we don't know these values and therefore we must separate the declaration
instruction from the effective fill of the array. That's how we proceed
in that case. So we begin by declaring our variable of array type,
here a fixed-size array of integers. Then, we must allocate the memory
locations, reserve memory locations for our array. This is done in the
following manner: we use the reserved word "new"; we then indicate
the type of the values that we want to store in the array, then in
brackets, the size of the array when it was constructed.
So what you must know is that once the size is allocated,
this size can't change during the program's execution, that's why
we call these arrays, fixed-size arrays.
So second step, we must allocate the reserved memory locations
for the array. At this stage, I'm in the following situation:
I have a variable "score" that contains a reference, an indirection, to an
array. This array contains four elements. What will these elements' values
be at this stage?
What you should know is that in Java there exists default
values that are put in arrays when they are constructed.
So for the previous situation we end up with an array
that contains zeroes. The default values are 0 for int and double types,
False for boolean, and there exists other dedicated values for other types
of data.
So concretely, when an instruction line of this nature is executed,
we have a situation in memory where the array looks like this.
The default values for each cell is zero because I am
working with an array of integers for which the default value
is indeed zero. Of course, these default values won't satisfy us in
most cases, so the third step will consist in
filling the array element by element, which implies taking interest in how
to access every cell of the array to put a dedicated value into it.
8:49
The mechanism by which I can access the i-th cell of an array is the
indexation mechanism. I use the following syntax: I indicate the
index, the number of the cell I want to access. What you must
know is that in Java, the index, the cells' number, varies between 0
and T - 1, where T is the size of the array. For example, if I declare an array of
integers "tab" in a program, and that I allocate an initial size of 10 to it,
as it is done here, then the first element
of the array corresponds to the index 0 and the last element of the array will
correspond to the index 9. With this notation, I can alter, modify
the content of a given cell, for example by writing this. It is
important to note that if you overflow the index of the intitially
allocated size, "an exception will be thrown", as we say.
At this stage of your learning, this will probably result in an
abrupt stop of your program. So here for example,
if I try and access a cell that is outside the licit indexes, given
that the last licit index is 9 for an array of size 10, we'll get an
error during the program's execution.
10:14
To come back to the initialization of arrays, here's a small example showing
how to fill an array element by element using the indexation
mechanism. This presupposes that we have declared the array, that we
constructed, beforehand. During the construction, we indicate the array's
size and that size can't change during the program's
execution, that's why we talk about fixed-size arrays.
Then, using indexation, it is possible to fill the array cell
by cell, like we do in this sequence of instruction lines.
Here, we'll finally reach this memory situation: the variable
"score" contains a reference to an array that looks like this.
The array is such that at the cell of index 0, there's the value 1000. It's what
we initialized with this instruction line, and at the cell of index 3,
the last cell, we have the value 6450, it's what we
obtained through this instruction line.