tcTitleRunning
tcTitleSlide
Code
Board
Trace
Description
Objects are similar to arrays of primitive types.
We will be using very simple object Coor1D
package codes.teaching.java.visualization;
public class Coor1D {
int x;
public Coor1D(int x) {
this.x = x;
}
...
}
We start with empty symbol table and memory.
You can run the code by clicking
Switch to code button.
// simple object composed of primitive types
No variable does exist.
c1
nor c2
.Check the trace.
u
indicates that the variable is undeclaredCoor1D c1;
declares variable c1
of type Coor1D
.
Declaration causes two actions
an entry in the symbol table
Note
c1
Coor1D
Coor1D
will have this color.
Coor1D c1;
declares variable c1
of type Coor1D
.
Declaration causes two actions
an entry in the symbol table
Note
c1
Coor1D
Coor1D
will have this color.
an entry in the memory
Note
-
in it.
-
indicates that the variable is uninitialized.Coor1D
will have this color.Coor1D c1;
declares variable c1
of type Coor1D
.
Declaration causes two actions
an entry in the symbol table
Note
c1
Coor1D
Coor1D
will have this color.
an entry in the memory
Note
-
in it.
-
indicates that the variable is uninitialized.Coor1D
will have this color.This is the code of Coor1D
:
package codes.teaching.java.visualization;
public class Coor1D {
int x;
public Coor1D(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public String toString() {
return "(" + x + ")";
}
public static void main(String[] args) {
Coor1D cA = new Coor1D(1);
System.out.println(cA);
}
}
c1 = new Coor1D(1);
causes two steps of actions:
new
always means creation of a new object.
In this case, a new object of type Coor1D
is created by calling the constructor
public class Coor1D {
int x;
public Coor1D(int x) {
this.x = x;
}
...
}
of Coor1D
by means of
... new Coor1D(1)
c1 = new Coor1D(1);
causes two steps of actions:
new
always means creation of a new object.
In this case, a new object of type Coor1D
is created by calling the constructor
public class Coor1D {
int x;
public Coor1D(int x) {
this.x = x;
}
...
}
of Coor1D
by means of
... new Coor1D(1)
c1
by means of
c1 = new ...
In this example,
Coor1D c2 = new Coor1D(2);
is equivalent to
Coor1D c2
c2 = new Coor1D(1);
combined and done in one step.
It does multiple operations at once:
declares variable c2
of type Coor1D
.
creates a new object of type Coor1D
at location 11.
finally, associates c2
with the newly created object.
Note the similarity of
c2
-@11-2c1
-@12-1c1 = c2;
is a manipulation of references.
c1
refers to Coor1D
object at location 12.c1 = c2;
is a manipulation of references.
c1
refers to Coor1D
object at location 12.c1
refers to Coor1D
object at location 11.Note that Coor1D
object at location 11 can be access by both c1
and c2
.
c1 = c2;
is a manipulation of references.
c1
refers to Coor1D
object at location 12.c1
refers to Coor1D
object at location 11.Note that Coor1D
object at location 11 can be access by both c1
and c2
.
Note that
Coor1D
object at location 12 now
What happens to Coor1D
object at location 12?
If this was the last reference to it,
System
When systems decides it is time to do garbage collection,
We will see that in steps 14 and 15.
See
We focus c1
for the next couple of statements.
c1.setX(9);
changes the coordinate of c1
.
What would be the result of
System.outprintln(c1);
System.outprintln(c2);
Try out.
This innocent move also changes the coordinate of c2
, too.
Check the trace.
c1 = new Coor1D(3);
is similar to
c1 = new Coor1D(1);
Coor1D
objectc1
to this new one.Note that
c1
refers to new objectc2
is not changed and keep referring to the previous one.What would be the result of
System.outprintln(c1);
System.outprintln(c2);
Try out.
c1.setX(8);
is the same statement previously used,
except the parameter is set to 8
:
c1.setX(9);
Both change the coordinate of c1
. But the effects of the statement is different at this time.
The important point is that only the object at location 10, is changed.
c1
,Why is the object at location 11 not changed?
It is time to consider garbage collection.
Now focus on location 12.
Suppose some more statements execute.
At some point
The garbage collector runs and
That's it.
...
// simple object composed of primitive types
Coor1D c1;
c1 = new Coor1D(1);
Coor1D c2 = new Coor1D(2);
c1 = c2;
c1.setX(9);
c1 = new Coor1D(3);
c1.setX(8);
...
. 2 3 3 3 4 4 5 6 6 6 7 8 9 10 10 .
Statement | c1 c2 ------------------------------------ | --- --- // simple object ... | u u Coor1D c1; | - u c1 = new Coor1D(1); | (1) u Coor1D c2 = new Coor1D(2); | (1) (2) c1 = c2; | (2) (2) c1.setX(9); | (9) (9) c1 = new Coor1D(3); | (3) (9) c1.setX(8); | (8) (9)