7 min read

Question 1: Primitive Types vs Reference Types

Link to MCQ Question

Situation: You are developing a banking application where you need to represent customer information. You have decided to use both primitive types and reference types for this purpose.

Part a

Define primitive types and reference types in Java. Provide examples of each.

Primitive types are basically boolean and numeric types such as char, byte, short, int, long, float, and double. Reference types are references to actual data which allows for us to access objects stored in another place

Part b

Explain the differences between primitive types and reference types in terms of memory allocation and usage in Java programs.

Part c

Lesson 1 Hacks

Doing the hacks found on this lesson

Part 1

public class Person {
    String name;
    int age;
    int height;
    String job;

    public Person(String name, int age, int height, String job) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.job = job;
    }
}

public static void main(String[] args) {
    Person person1 = new Person("Carl", 25, 165, "Construction Worker");
    Person person2 = new Person("Adam", 29, 160, "Truck Driver");
    Person person3 = person1;
    int number = 16;
    System.out.println(number);
}
main(null);
Failed to start the Kernel. 


Error: Unable to access jarfile /home/edwin/.local/share/jupyter/kernels/java/ijava-1.3.0.jar. 


View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.

Answer the following questions based on the code above:

1) What kind of types are person1 and person2?

person1 and person2 are both objects from the Person class and can have attributes assigned to them from name, age, height, and job.

2) Do person1 and person3 point to the same value in memory?

person1 and person3 point to the same value in memory

3) Is the integer ‘number’ stored in the heap or in the stack?

The integer ‘number’ is probably stored in the stack because it is being called while it is being stored I believe, so that means that after the method is called and has finished running, the number value is cleared.

4) Is the value that ‘person1’ points to stored in the heap or in the stack?

The value that person1 points to is stored in the heap because it once the application is running, it needs to get memory to store those values.

Part 2

Seen above as the FRQ

b Do person1 and person3 point to the same value in memory? c Is the integer ‘number’ stored in the heap or in the stack? d Is the value that ‘person1’ points to stored in the heap or in the stack?

Part 2

Seen above as the FRQ