tcTitleRunning
tcTitleSlide
Code
Board
Trace
Description
We start with empty
Note that in trace
u means undefined variable- means defined but uninitialized variable? means your turn to answerAt this point all variables are undefined.
int a;
is a declaration statement. It causes a number of actions:
a
inta
a is defined but its content is uninitialized at this point. It is
indicated by u.
In order to simplify the picture, we use
aaIn reality, run time system assigns
a = 4;
is an assignment statement.
a to 4.int[] arrInt;
is similar to int a;.
arrInt of type int[],
intIt causes:
arrInt = new int[3];
0.
0Note that
arrInt refers to these 3 locations all together.
individual locations are referred by
arrInt[0] for location 9,arrInt[1] for location 10,arrInt[2] for location 11arrInt has a value. The value indicates the memory locations reserved for arrInt.
[I@33909752, indicating that it is an
array of integers ([I) and some number (33909752) for actual
location after @Note that
arrInt = new int[2];
For simplification, we used location 9 for the new array
In reality, the runtime system would use the first available location in the heap
see
Now focus on arrInt[0].
arrInt[0] = 8;
sets its content to 8.
a = 4;
a to 4.Similarly,
arrInt[2] = 5;
sets the content of arrInt[2] to 5.
Note that
1, that is arrInt[1]So does this:
arrInt[1] = 7;
So far we use numbers on the right hand side of assignments.
Let's try something new.
Focus to arrInt[0] again.
arrInt[0] = a;
a,
arrInt[0]arrInt[0] changesDo you thing value of a changes?
No. The content of a does not change.
a to
arrInt[0]a to
arrInt[0]a = 9;
a,
arrInt[0] stays unchanged.It is possible to use two elements of array as any ordinary variables.
arrInt[0] = arrInt[2];
This is actually no different than
b = a;
Now we are about to do something very new.
2 byarrInt = new int[2];
As a result,
arrInt[2]?arrInt[0] and arrInt[1]?
Let's run
arrInt = new int[2];
Did you expect this?
Java does an unexpected thing.
arrInt has a new value.arrInt has a new location in the memory.How does the system decide on location 7?
new operator constructs the new array starting not at location
7.
arrInt[0] = 1;
As we previously did, we can assign new values to the items.
Note that arrInt[0] is in the new location of the array.
So does
arrInt[1] = 2;
Do you want to check the trace? Go to step 0 by clicking on 0 in navigation.
That's all.
...
int a;
a = 4;
//
int[] arrInt;
//
arrInt = new int[3];
arrInt[0] = 8;
arrInt[2] = 5;
arrInt[1] = 7;
//
arrInt[0] = a;
a = 9;
//
arrInt[0] = arrInt[2];
//
arrInt = new int[2];
// pause
arrInt[0] = 1;
arrInt[1] = 2;
...
1
2
3
5
7
8
9
10
12
13
15
17
17
19
20
.
statement | a arrInt
\index | 0 1 2
---------------------- + - - - - ------
| u u u u u
int a; | - u u u u
a = 4; | 4 u u u u
int[] arrInt; | 4 u u u -
arrInt = new int[3]; | 4 0 0 0 [I@33909752
arrInt[0] = 8; | 4 8 0 0 [I@33909752
arrInt[2] = 5; | 4 8 0 5 [I@33909752
arrInt[1] = 7; | 4 8 7 5 [I@33909752
arrInt[0] = a; | 4 4 7 5 [I@33909752
a = 9; | 9 4 7 5 [I@33909752
arrInt[0] = arrInt[2]; | 9 5 7 5 [I@33909752
arrInt = new int[2]; | 9 0 0 u [I@55f96302
arrInt[0] = 1; | 9 1 0 u [I@55f96302
arrInt[1] = 2; | 9 1 2 u [I@55f96302