Advertisement
therrontelford

Array memory locations

Jan 15th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1.  
  2. public class memoryLocations {
  3.  
  4.     public static void main(String[] args) {
  5.         // memory locations
  6.         int [] array = {1,2,3,4,5};
  7.        
  8.         int [] arr = {2,5,4,9,8,1};
  9.        
  10.         // makes a copy of arr and stores it in new array named copy
  11.         int [] copy = new int[6];
  12.         System.arraycopy(arr,0,copy,0,arr.length);
  13.        
  14.        
  15.         System.out.println(array);
  16.         System.out.println("it prints the memory location of array");
  17.         for (int e : array)
  18.             System.out.print(e+" ");
  19.        
  20.         System.out.println(); //skip a line
  21.        
  22.         System.out.println(arr);
  23.         System.out.println("It prints the memory location of arr");
  24.         for (int e : arr)
  25.             System.out.print(e+" ");
  26.        
  27.         System.out.println();
  28.        
  29.         System.out.println(copy);
  30.         System.out.println("It prints the memory location of copy");
  31.         for (int e : copy)
  32.             System.out.print(e+" ");
  33.        
  34.         System.out.println();
  35.        
  36.         arr = array;  // array "arr" gets array "array"
  37.         System.out.println("array \"arr\" gets array \"array\"");
  38.        
  39.         System.out.println(array);
  40.         System.out.println("the memory location of array is unchanged");
  41.         for (int e : array)
  42.             System.out.print(e+" ");
  43.        
  44.         System.out.println();
  45.        
  46.         System.out.println(arr);
  47.         System.out.println("arr is now pointing to the same reference as array");
  48.         for (int e : arr)
  49.             System.out.print(e+" ");
  50.        
  51.    
  52.        
  53.  
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement