Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following program in Java: class T{ A foo(B c, A d){ 5 c = new B();

ID: 3716622 • Letter: C

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 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 let’s 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 …

Explanation / Answer

NOTE: I have completed your assignment and below are the details. Please check and let me know if you have any questions. I will revert back within 24 hours.

Explanation:
3) t: 0x00000012, a: ?, b: null, m: ?, c: ?, d : ?, final output: ? // object b is assigned null here. Other values unknown
4) t: 0x00000012, a: ?, b: null, m: A@4e25154f, c: ?, d: ?, final output: ? // object b set to null and there is an address assigned to m
5) t: 0x00000012, a: A@70dea4e, B@4e25154f, m : ?, c : 0x00000037, d : A@70dea4e, final output: ? // here the values of c and d are references of objects b and a passed to the function by the caller.
6) t: 0x00000012, a: A@70dea4e, B@4e25154f, m : ?, c : 0x00000037, d : A@70dea4e, final output: ? // there wont be any difference as member variable changed not the address.
7) t: 0x00000012, a: A@70dea4e, B@4e25154f, m : ?, c : 0x00000037, d : A@70dea4e, final output: ? // No change as only member variables of the object changed.
8) t: 0x00000012, a: A@70dea4e, B@4e25154f, m : ?, c : 0x00000037, d : A@70dea4e, final output: ? // No change as only member variables of the object changed.
9) The value printed in line 9 is 23