Background information:   why an array of objects is confusing in Java  

  • A primitive type variables can be used immediately in computations:

      double x = 1;
    
      x = x + 1;
    

  • When we instantiate an array of primitive type variables:

      double numbers = new double[10];
    

    we can use the array immediately in computations:

      numbers[0] = 1;
      numbers[1] = 4;
      ...
    


  • That is different with reference data types

  Array of primitive type variables contains (multiple) numbers that can be used in computations  

E.g.:   a double variable contains a number, so an array of double contains multiple numbers

   double[] numbers; 
 
          Result:   

   numbers = new double[100];
      
          Result:    

Note:   numbers[i] is a double variable and can store a number

  Array of reference type variables contains (multiple) references that point to (different) objects  

E.g.:   a Circle variable contains a reference, so an array of Circle contains multiple references

   Circle[] circleArray; 

          Result:   

   circleArray = new Circle[10];
                         
          Result:    

Note:   circleArray[i] is a Circle reference variable and can store a reference to a Circle object

  Array of reference type variables contains (multiple) references that point to (different) objects  

In other words:   we can create a Circle object with new and assign it to an array element ( circleArray[0])

   circleArray = new Circle[10];
                             
      Result:        

   circleArray[0] = new Circle(4);
                             
      Result:          

Contrasting an array of primitive type variables and an array of reference type variables

What is an array of primitive typed variables and an array of reference variables:

  • After creating an array of primitive variables, each array element can store a value:

        double[] numbers = new double[100];
    
        numbers[0] = 99; // We can store a number
     

  • After creating an array of reference variables, each array element can store a reference of an object:

        Circle[] circleArray = new Circle[10];
    
        circleArray[0] = new Circle(2.0); 
                 // Stores a reference to a circle object
    
    

Contrasting an array of primitive type variables and an array of reference type variables

The use of primitive type variables and reference type variables are different:

  • Primitive type array variables (number[k]) contains values and is used in computations:

        double[] numbers = new double[100];
    
        numbers[0] = 99;
        sum = sum + numbers[0]; // number[0] is a double

  • Reference type array variables (circleArray[k]) contains references and is used with the member selection operator  . :

        Circle[] circleArray = new Circle[10];
    
        circleArray[0] = new Circle(2.0); 
        double area = circleArray[0].getArea();
                      // circleArray[0] is a Circle reference

Using an array of objects

  • Array of objects is frequently used in computer programs

  • In the next set of slides, I will show you how to use an array of Card to represent a deck of playing cards