Question: Consider the following program in Java: class T{ A foo(B c, A d){ 5 c = new B(); // The address of this object is

Consider the following program in Java:

class T{

A foo(B c, A d){

5 c = new B(); // The address of this object is 0x00000037

6 c.intField = 23;

7 d.bField =c;

8 return d;

}

}

class A {

public B bField;

}

class B{

public int intField;

public void main(String[] args){

1 T t = new T(); // Suppose the heap address of the object is 0x00000012

2 A a = new A(); // The address of this object is 0x00000025

3 B b = null;

4 A m = t.foo(b, a);

9 Systems.out.println(a.bField.intField);

return;

}

}

1.We all know that Java is a call-by-value language. Write down the values of the variables t, a, b, m, c, d at each indexed program point as well as the final output (i.e., the value printed in line 9).

2.Now lets pretend that Java is a call-by-reference language. Write down the values of these six variables at each program point as well as the final output.

An example solution:

1 t: 0x00000012, a: ?, b: ?, m: ?, c: ?, d?, final output: ? (? means the value is unknown.)

2 t: 0x00000012, a: 0x00000025, b: ?, m: ?, c: ?, d?

3

4

5

6

7

8

9

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!