Chapter 13
Code Execution
- We are interested in understanding how a given Java code is executed.
- We care to have a model explaining the execution.
- Details that may obstruct our understanding are ignored.
- In particular, imported features are considered to be black boxes: well understood behavior, but no knowledge of the
internal parts.
13.1 Control Flow
- The command ‘java prog’ states the class in which the execution should start, namely, ‘prog.class’
- The execution starts at a method having the signature of ‘main ( String[] )’.
- Upon reaching a constructor or a method name, the control passes to that entity.
- A program execution can be terminated anywhere with the ‘System.exit(0)’ command
class prg {
public static void main ( String [] args ){
int iVar;
MyClass cVar;
cVar = new MyClass();
MyClass.cMethod();
iVar = cVar.iMethod();
System.exit(0);
} }
class MyClass {
MyClass (){
System.out.println( "a constructor" );
}
static void cMethod(){
System.out.println( "a class method" );
}
int iMethod(){
System.out.println( "an instance method" );
return 0;
}
}
13.2 Inside Constructors
class prg {
public static void main ( String[] args ){
MyClass a = new MyClass(12);
} }
class MyClass {
int f1;
double f2 = 0.0001;
MyClass (int i){ }
}
When invoked, a constructor
- Creates an object whose memory consists of instance fields
- Initializes the content of the fields to the specified values, or to default values when none is specified.
- Allocates memory to a local system variable named this, and stores there a pointer to the object
- Allocates memory to the parameters, and initializes them to the values provide by the arguments.
- Executes the body of the constructor.
- Removes the parameters and local variables allocated within the constructor.
- Returns to the calling point a pointer to the created object
13.3 Inside Instance Methods
class prg {
public static void main ( String[] args ){
MyClass a = new MyClass();
int i = a.myMethod(25);
} }
class MyClass {
int f1;
double f2 = 0.0001;
int myMethod (int x){
return 7; }
}
When invoked, an instance method
- Gets from the calling point a pointer to an object of the class.
- Allocates memory to a local system variable named this, and stores there a pointer to the given object
- Allocates memory to the parameters, and initializes them to the values provide by the arguments.
- Executes the body of the method
- Removes the variables allocated within the method.
- Returns to the calling point. In case the method has a non-void return type, the return commands sends a value to
the calling point.
13.4 Inside Class Methods
13.5 Objects and Fields
- A class provides class fields and instance fields for carrying data through computations
- The instace fields are embedded within objects.
- An object of a class is said to be an instance of the class
- Objects of a class are requested through constructors.
- The constructors return pointers to the objects
class prg {
public static void main ( String[] args ){
MyClass n1 = new MyClass();
MyClass n2 = new MyClass();
} }
class MyClass {
MyClass (){ }
}
- Each object is allocated a, possibly empty, set of the instance fields
class prg {
public static void main ( String[] args ){
ClassA a1 = new ClassA();
ClassA a2 = new ClassA();
ClassB b1 = new ClassB();
ClassB b2 = new ClassB();
} }
class ClassA {
int f1;
double f2;
ClassA (){ }
}
class ClassB {
ClassB (){ }
}
- A program has one copy of each class field, and for each instance one copy of instance fields
class prg {
public static void main ( String[] args ){
MyClass a1 = new MyClass();
MyClass a2 = new MyClass();
} }
class MyClass {
static int f1;
int f2;
MyClass (){ }
}
- Instance fields can be only accessed through the object to which they belong.
class prg {
public static void main ( String[] args ){
MyClass a1 = new MyClass( 11 );
MyClass a2 = new MyClass( 22 );
a1.myMethod( 33 );
} }
class MyClass {
int num;
MyClass ( int v ){ this.num = v; }
void myMethod ( int v ){ this.num = v; }
}
13.6 Life Span of Variables and Objects
- For each class, a program provides one copy of the class fields.
- The class fields are available throughout the computation of the program
- For each object, the program has one copy of the instance fields.
- The instance fields of an object are available as long as the object is accessible
- Objects are inaccessible if no variable references them.
- Java periodically runs a garbage collector to reclaim the memory allocated to objects that are not in use anymore
class prg {
public static void main ( String[] args ){
MyClass n1 = new MyClass();
n1 = new MyClass();
} }
class MyClass {
class int f1;
int f2;
MyClass (){ }
}
- Parameters are allocated when constructors and methods are entered, and deallocated when they are
left
class prg {
public static void main ( String[] args ){
MyClass x = new MyClass( "abc" );
x.foo( 1, 1.2);
} }
class MyClass {
int f1;
double f2;
MyClass ( String s ){ }
classic void foo(int a, double b){}
}
- The ‘this’ local variable is introduced when constructors and instance methods are entered, and deallocated upon
exit
- Local variables are alive from the point of declaration to the end of their group.
class prg {
public static void main ( String[] args ){
int a;
a=1;
{ int b;
a=2; b=3;
}
a=4;
int c;
a=5; c=6;
} }
13.7 Data Flow
- Upon entering a parametric constructor or a parametric method, the parameters get the values of the corresponding
arguments
- Upon entering a constructor or an instance method, the ‘this’ variable is assigned the pointer to the instance object
in discourse.
- Upon returning from a constructor, a pointer to the object is returned
- Upon returning from a non-void method, the return value is sent back.
- Class fields can be accessed only through the class they belong to
- Instance fields can be accessed only through the instance to which they belong
13.8 String Values
- A class can provide string representations to its objects by defining a method having the interface of ‘public
String toString()’.
class prg {
public static void main ( String[] args ){
MyClass a;
a = new MyClass( "my name" );
System.out.println( a );
} }
class MyClass {
String name;
MyClass( String s ){ name = s; }
public String toString(){
return name;
}
}
- The ‘java.lang.Object’ class introduces a definition for ‘toString’, used as a default for settings not providing
alternative realizations. In that realization, the objects are represented by strings which start with the class name
and the at-sign character ‘@’.
class prg {
public static void main ( String[] args ){
MyClass a;
a = new MyClass( "my name" );
System.out.println( a );
} }
class MyClass {
String name;
MyClass( String s ){ name = s; }
}
Exercises
13.9 Assignment #14: Objects: Issues of Syntax
Due: Mo, May 12, midnight
Consider the following program.
class lab14 {
public static void main ( String[] args ){
Person me, you, other;
me = new Person( "my name", "my address", "me@here", 1234567 );
you = new Person( "your name", "your address", "you@there", 7654321 );
other = new Person( "other name", "", "", -1 );
other.changeAddress( "other address" );
other.changeEmail( "other@where" );
other.changePhone( 1357642 );
System.out.println( me );
System.out.println( you );
System.out.println( other );
} }
Add to the program the missing class, so that the program will compile under ‘javac lab14.java’ and
execute under ‘java lab14’. Don’t put any instructions within the bodies of the constructors and
methods.
The execution of the program should provide output similar to:
Person@720eeb
Person@3179c3
Person@310d42
Assume ‘lab14’ for the submit program. The program should be stored in a file named ‘lab14.java’.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A
13.10 Assignment #15: The Working of Constructors and Methods
Due: We, May 14, midnight
Consider the following program.
class lab15 {
public static void main ( String[] args ){
Person me, you, other;
me = new Person( "my name", "my address", "me@here", 1234567 );
you = new Person( "your name", "your address", "you@there", 7654321 );
other = new Person( "other name", "", "", -1 );
other.changeAddress( "other address" );
other.changeEmail( "other@where" );
other.changePhone( 1357642 );
System.out.println( me );
System.out.println( you );
System.out.println( other );
} }
Add to the program the missing class, so that its output will equal:
New Object:
-----------
this = Person@256a7c
name = my name
address = my address
email = me@here
number = 1234567
New Object:
-----------
this = Person@720eeb
name = your name
address = your address
email = you@there
number = 7654321
New Object:
-----------
this = Person@3179c3
name = other name
address =
email =
number = -1
Change Address:
---------------
this = Person@3179c3
new address = other address
Change Email:
-------------
this = Person@3179c3
new email = other@where
Change Number:
--------------
this = Person@3179c3
new number = 1357642
Person@256a7c
Person@720eeb
Person@3179c3
Assume ‘lab15’ for the submit program. The program should be stored in a file named ‘lab15.java’.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A
13.11 Assignment #16: Objects: Issues of Semantics
Due: We, May 21, midnight
Consider the following program.
class lab16 {
public static void main ( String[] args ){
Person me, you, other;
me = new Person( "my name", "my address", "me@here", 1234567 );
you = new Person( "your name", "your address", "you@there", 7654321 );
other = new Person( "other name", "", "", -1 );
other.changeAddress( "other address" );
other.changeEmail( "other@where" );
other.changePhone( 1357642 );
System.out.println( me );
System.out.println( you );
System.out.println( other );
} }
Add to the program the missing class, so that the execution of the program would provide the following
output.
my name, my address, me@here, 1234567
your name, your address, you@there, 7654321
other name, other address, other@where, 1357642
The only methods allowed to be invoked in the class are ‘concat’ from java.lang.String and ‘toString’ from
java.lang.Integer.
Assume ‘lab16’ for the submit program. The program should be stored in a file named ‘lab16.java’.
[practice example]
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
13.12 Assignment #17: Objects
Due: Fr, May 23, midnight
Consider the following program.
class lab17 {
public static void main(String[] args){
Student a, b, c, d, e;
a = new Student( "Dan" );
b = new Student( "Evy" );
c = new Student( "Eli" );
d = new Student( "Ron" );
a.friend(d);
b.friend(c);
c.friend(a);
d.friend(b);
Student.showStudents();
e = a;
System.out.println( e );
e = e.friendOf();
System.out.println( e );
e = e.friendOf();
System.out.println( e );
e = e.friendOf();
System.out.println( e );
}
}
Add to the program the missing class, so that the execution of the program would provide the following
output.
Dan Evy Eli Ron
Dan
Ron
Evy
Eli
The only methods allowed to be invoked in the class are ‘concat’ from java.lang.String and ‘toString’ from
java.lang.Integer.
Assume ‘lab17’ for the submit program. The program should be stored in a file named ‘lab17.java’.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.