Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class memoryLocations {
- public static void main(String[] args) {
- // memory locations
- int [] array = {1,2,3,4,5};
- int [] arr = {2,5,4,9,8,1};
- // makes a copy of arr and stores it in new array named copy
- int [] copy = new int[6];
- System.arraycopy(arr,0,copy,0,arr.length);
- System.out.println(array);
- System.out.println("it prints the memory location of array");
- for (int e : array)
- System.out.print(e+" ");
- System.out.println(); //skip a line
- System.out.println(arr);
- System.out.println("It prints the memory location of arr");
- for (int e : arr)
- System.out.print(e+" ");
- System.out.println();
- System.out.println(copy);
- System.out.println("It prints the memory location of copy");
- for (int e : copy)
- System.out.print(e+" ");
- System.out.println();
- arr = array; // array "arr" gets array "array"
- System.out.println("array \"arr\" gets array \"array\"");
- System.out.println(array);
- System.out.println("the memory location of array is unchanged");
- for (int e : array)
- System.out.print(e+" ");
- System.out.println();
- System.out.println(arr);
- System.out.println("arr is now pointing to the same reference as array");
- for (int e : arr)
- System.out.print(e+" ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement