Description
Memory Mapping Worksheet
1. Draw the memory map of the following int and one-dimensional array of type int.
int w = 14;
int[] x = new int[8];
2. Draw the memory map of the following two-dimensional ragged array of int
int[][] y = {{4, 8, 15}, {16, 23, 42, 10}, {8, 30}};
3. Draw the memory map of the following one-dimensional array of type String.
String[] z = new String[4]; for (int i = 0; i < z.length; i++) { z[i] = “element ” + i;
}
4. a. Write a shallow copy of the following in code. (Assume the five animal objects are already instantiated.)
Animal[] zoo = {tiger1, elephant2, giraffe3, monkey4, ape5}; Animal[] copy;
copy=zoo;
b. Draw the memory map.
Stack: Heap:
5. a. Write the deep copy of the following in code.
Animal[] zoo = {tiger1, elephant2, giraffe3, monkey4, ape5};
Animal[] copy;
copy = new Animal[zoo.length]; for(int i = 0; i < copy.length; i++){
copy[i] = new Animal(zoo[i]);
}
b. Draw the memory map.
6. What is garbage collection? Where does it happen?
The garbage collector is a program, which runs on the Java Virtual Machine that gets rid of objects not being used by a Java application anymore. The garbage collector will look for objects which aren’t being used anymore and gets rid of them.
A concurrent GC runs in the background, cleaning up while the program is running. Some GC’s might run as part of every memory allocation.
7. What is the difference between the two operators, equals() and ==?
The main difference between == and equals() in Java is that “==” is used to compare primitives while equals() method is recommended to check equality of objects.
== returns true only if both references point to the same object
4 | P a g e
equals() can return true or false based on its overridden implementation.
Reviews
There are no reviews yet.