Defining array variables in Java

  • Syntax to define an array (reference) variable:

     dataType[] arrayRefVar;   // Defines an array reference variable

  • Syntax to create an array object:

     new dataType[N]; // Create an array of N elements 

  • Properties of an array in Java:

    • All elements in the array have the same data type.

    • Array elements are stored consecutively in memory

  • Example: define an array of 10 int variables

       int[] A; 
       A = new int[10];

What happens inside the computer when you define an array

The following diagrams show what happens inside the computer system:

     int[] A;  // A is a reference variable !
     A = new int[10];

"int[] A" will allocate (reserve memory) a reference variable A:

What happens inside the computer when you define an array

The following diagrams show what happens inside the computer system:

     int[] A;  // A is a reference variable !
     A = new int[10]; 

"new int[10]" will allocate (reserve memory) for an int[10] array (= 40 bytes) and return its base address:

What happens inside the computer when you define an array

The following diagrams show what happens inside the computer system:

     int[] A;  // A is a reference variable !
     A = new int[10]; 

"A = " will assign the return value to the variable A:

What happens inside the computer when you define an array

Summary of the effect of:

     int[] A;  // A is a reference variable !
     A = new int[10];

The reference variable A will point to a newly created int[10] (array of 10 integers) object:

Copying an array

  • Copy an array means:

      • Make a duplicate of an array where the duplicate contains the same data as the original

      • Updating array elements in the duplicate must not affect the data in the original array

  • Schematically:   situation before making a copy of the array myList

Copying an array

  • Copy an array means:

      • Make a duplicate of an array where the duplicate contains the same data as the original

      • Updating array elements in the duplicate must not affect the data in the original array

  • Schematically:   situtaion after making a copy of the array myList

How to copy an array in Java    step-by-step instructions

  • Initial state:

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
           
            double[] myListCopy = new double[ myList.length ];
            
            for ( int i = 0; i < myList.length; i++ )
                myListCopy[i] = myList[i];
        } 

How to copy an array in Java    step-by-step instructions

  • Step 1: create an array to store the copy

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
          
            double[] myListCopy = new double[ myList.length ];
     
            for ( int i = 0; i < myList.length; i++ )
                myListCopy[i] = myList[i];
        } 

How to copy an array in Java    step-by-step instructions

  • Step 2: copy the array elements to the copy array

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
          
            double[] myListCopy = new double[ myList.length ];
     
            for ( int i = 0; i < myList.length; i++ )
                myListCopy[i] = myList[i];
        } 

DEMO: demo/02-review/CopyArray.java

A common error in array copy

  • Variables of primitive data types (such as int, double, etc) can be copied with an assignment:

        int x = 4;
        int xCopy;
    
        xCopy = x;   // Copies x to xCopy 

  • However, in Java, the assignment operation will not copy objects of non-primitive data types:

        double[] myList = {34, 15, 66, 7};
    
        double[] myListCopy;
    
        myListCopy = myList;   // Does not copy an array object 
                               // ** This copies the reference
    			   // ** in myList to myListCopy

The effect of assignment with a reference variable

  • Initially we have the array myList:                                                                                    

        double[] myList = {34, 15, 66, 7};
    
    
       
         

    Schematically:

     

The effect of assignment with a reference variable

  • The definition double[] myListCopy defines another reference variable:                                        

        double[] myList = {34, 15, 66, 7};
    
        double[] myListCopy;
    
     

    Schematically:

     

The effect of assignment with a reference variable

  • The assignment myListCopy = myList will copy the reference in myList to the myListCopy variable:

        double[] myList = {34, 15, 66, 7};
    
        double[] myListCopy;
    
        myListCopy = myList;   // Copies an reference  

    Schematically:

    Now myList and myListCopy both reference to the same array object

The effect of assignment with a reference variable

  • Because myListCopy and myList reference the same array object, updates made with myListCopy[i] will also affect myList[i] and vice verse:

    public class Alias
    {
        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
    
            double[] myListCopy;
    
            myListCopy = myList; // Does not copy an array object 
    
    	myListCopy[1] = 999; // Update will also affect myList[1]
    
    	for ( int i = 0; i < myList.length; i++ )
    	    System.out.print( myList[i] + " " );
        }  
    }

Demo: demo/02-review/Alias.java

  Alias  

  • We saw in the last example that: myListCopy[1] and myList[1] were the different names for the same (array) variable:

        public static void main(String[] args) 
        {
            double[] myList = {34, 15, 66, 7};
    
            double[] myListCopy;
    
            myListCopy = myList; // Does not copy an array object 
    
    	myListCopy[1] = 999; // Update will also affect myList[1]
        }  

  • Alias:

      • When a different variable names can be used to reference the same variable, they are called aliases in computer science