Archibald Haddock (123456789) COMP-202A, Section 0 (Fall 2010) Instructor: Cuthbert Calculus Assignment 3, Question 1b Dear friend's little brother, You are incorrect when you say that the program below prints 7, since method1 defines a new scope that is distinct from that of main. This means that the i inside method1 refers to a different piece of data in memory than the i inside method main. Here's what happens in memory at each line of the program: public class Test{ public static void main(String[] pants){ // In the following line, i represents the content of a memory location x_a that // contains 3, and // j represents the content of a memory location x_b that contains 4. int i = 3, j = 4; // In the following line, we call method1 on values 3 and 4, so this creates a // new scope: see my comments in method1 for an explanation of what happens in // this scope. // method1 returns a value, but we do not assign that value to anything, so it // is lost. In other words, the expression "method(i,j)" has value 7, but this // value isn't save anywhere and never used again. method1(i,j); // In the following line, we display the value stored in the i that was defined // above, in main. That is, we display the value stored at memory location x_1, // which is 3. System.out.println(i); } // This is what happens whenever a new memory scope is created for method1 (i.e. // whenever method1 is called). // Suppose we call method1(3,4), as above in main. // First, a variable i in method1's scope is made to represent the content // of a memory location // x_c that contains 3.0. Note that this is different from the memory location x_a // whose content is represented by i in main. // Then, a variable j in method1's scope is made to represent the contant of // memory location // x_d that contains 4.0. Again, this is different than x_b, whose content is // represented by j in // main's scope. // Note that 3 and 4 given in main are automatically promoted to 3.0 and 4.0 in // method1 -- this is assignment conversion. private static double method1(double i, double j){ // In the following line, we add the values in memory locations x_c and x_d // (namely 3.0 and 4.0), and store that in memory location x_c. Note that memory // location x_a, which corresponds to i in main, is NOT affected. i = i + j; // In the following line, the value inside memory location x_c is returned to // the calling function. This means that the expression "method1(3,4)" has value // 7.0. // THEN, the memory scope for method1 is erased: the content of memory locations // x_c and x_d is deleted, and you can nolonger use variables i and j to access // the data in those memory locations. return i; } } On the other hand, you, my friend's little brother, must have thought that all the i's and j's in this code refer to the same data, so when i is set to i + j in method1, the i in main is also affected. Furthermore, you also forgot that the arguments to method1 are automatically converted to double.