FAQ CS1 Week 8

Muddiest Points from Lecture Material

  1. What is the difference between testing and debugging?

    When we test our program, we do so to find out IF it has any errors. When we debug our programs, we already know that there are errors and we try to use the information we know about the symptoms to FIND those errors.

  2. What's the difference between passing an array element to a method and passing an array?

    All arguments in Java are passed by value. This means that a method gets a COPY of the VALUE that is associated with the variable. For primitives, this is a COPY of the VALUE of the primitive. For objects, this is a COPY of the REFERENCE to the object.

    If the array elements are primitive types, say int, then when we pass an array element to a method that requires an int, the method's formal parameter receives a COPY of the VALUE of the array element, i.e., a copy of the int value. This is just like what happens when we pass any int to the method.

    If we pass an array to a method, then what is passed is a COPY of the REFERENCE to the array. (Note: An array is an object. When we declare an array, we only get a place to put an address/reference, not storage space for the elements of the array. To get the storage space for the elements of the array, we must instantiate the array.) The ramification of this is the same as if we were passing any object to a method, the contents of the object can be changed inside the method. In the case of an array, the contents of one or more array element may be changed. This is because the method has access to the contents of the object (in this case array elements) through the reference that was passed to it.

    I've created a little program ArrayPass.java (output) for you to try out.

    It should be noted that if the array were an array of objects, then passing an element of that array to a method would still leave us open for the contents of the object referred to by that array element being altered in that method....just like passing ANY other object to a method.

    Remember that you can download the files linked to the page by SHIFT - left-clicking on the link

  3. If I have a two dimensional array called arraOfNums and arrayOfNums.length tells me the number of rows in arrayOfNums, how can I find out the number of columns?

    Use arrayOfNums[ the_row_number ].length. Notice this means that different rows may have different lengths!

  4. I know that this has not been covered in class yet, but I am really confused about this. Yesterday I saw a program in which this code executed without error
    public static void main(String args[]){
    System.out.println( args.length )
    }
    I could not understand what this length refers to here as it does not have parentheses. I think args without any index refers to the first parameter, but what does this length variable refers to?

    In this case arg.length refers to the an instance constant for the args array (args is an array of String), whereas if we're talking about the length of a String string1, we are using a method, eg., string1.length(). The () tells us and the Java compiler that length is a method of the String class.

  5. What is meant by shallow and deep copies?

    Shallow copy and deep copy are terms that refer to copying containers of objects. For example, let's suppose we had an array of objects, Object [] a = new Object[ARRAY_SIZE];, that has been filled with objects of some sort. Suppose also that we need a second copy of this array and want to identify it as b; Think about what would happen if we said:

    Object [] b;
    b = a;
    This assignment would only provide us with a second reference to the SAME array and thus through the single array to the elements. This is NOT a copy.

    Our second approach might be to say

    Object [] b = new Object[a.length];

    for ( int i = 0; i < a.length; i++ ) {
       b[i] = a[i];
    }

    This approach gives a shallow copy of the array in b, in this case b is an array containing the same set of REFERENCES that the array a contains!

    To get a deep copy we must go AT LEAST one step further:

    Object [] b = new Object[a.length];

    for ( int i = 0; i < a.length; i++ ) {
       b[i] = new Object( a[i].makeDeepCopy( ));
    }

    where makeDeepCopy( ) is a method THAT MUST BE DESIGNED and DEFINED somehow. Notice that this is even more complex than it may first sound. Suppose the Objects that are stored in a have as one of their instance variables another Object or subclass of Object. For example, suppose that a is an array of a class of Students where a Student is an object that has as one of its instance variables a name, i.e., a String. If I try to make a deep copy of an elelment of a, i.e., one Student record, I must not only make a copy of the Student object referenced by a[i], I must also make a copy of the String object that is referred to by that Student object. And, I must do this for ALL objects not only in the Student object, but also for all objects that those objects reference, and so on. To state this another way, I would need to recursively make copies that all objects involved refer to!

    You'll be learning more about deep and shallow copies and recursion in future courses.


Nan C. Schaller
Rochester Institute of Technology
Computer Science Department
102 Lomb Memorial Dr.
Rochester, NY 14623-5608
telephone: +1.585.475.2139
fax: +1.585.475.7100
e-mail: ncs@cs.rit.edu
September 30, 2004
http://www.cs.rit.edu/~ncs/Courses/cs1/Muddy8.shtml