CPSC 425 Spring 2008
HW 14: Parameters

  1. Read Chapter 18 in Webber. We did not talk about macro expansion in class and you will not be responsible for the material in section 18.7, but you may find it interesting to read. You will be responsible for the rest of the material in the chapter.

  2. Do exercises 1, 2, and 6(a),(b),(c), and (e) in chapter 18.

  3. In Java all parameters are passed by value, that is, the value of the actual parameter is copied into the formal parameter at the time of the call. Consider these two examples:
    public class Test1
    {
    public static void main(String[] args)
    {
      int x = 1;
      f(x);
      System.out.println(x);
    }
    
    static void f(int y)
    {
      y++;
    }
    }
    ------
    public class Test2
    {
    public static void main(String[] args)
    {
      int[] x = new int[10];
      x[0] = 1;
      f(x);
      System.out.println(x[0]);
    }
    
    static void f(int[] y)
    {
      y[0]++;
    }
    }
    

    Test1 prints the value 1, but Test2 prints the value 2. Explain this in terms of Java's parameter passing mechanism and its int and array types.