Lesson 1.13 Homework
Answers Questioned
Question 1: Why does changing b
not affect a
, but changing array2
affects array1
?
a
and b
are primitive types.
When you assign b = a;
, the value 10
is copied.
Changing b
later does not affect a
because they are stored separately.
array1
and array2
are references to the same array in the heap.
When array2[0] = 99;
runs, both variables point to the same memory.
So, changing one changes the other.
Question 2: Describe what’s on the stack vs. the heap for this code
Stack:
a = 10
b = 20
array1 → 0x001
array2 → 0x001
Heap:
0x001 → [99, 2, 3]
The stack stores variables and references.
The heap stores the actual array.
Both array1
and array2
point to the same array in the heap.
Question 1: After haveBirthday(john)
is called, what is John’s age? Why?
After haveBirthday(john)
runs, John’s age is 21.
The method changes the value of p.age
, and since p
refers to the same Person
object as john
, the change affects that object directly.
In Java, object references are passed by value, but the value is a reference, so the method can modify the object’s fields.
Question 2: After reassignPerson(john)
is called, what is John’s name and age? Why?
After reassignPerson(john)
, John’s name is still “John” and his age is still 21.
Inside the method, p
is reassigned to a new Person
object, but this does not affect john
outside the method.
The reassignment only changes what the local variable p
points to, not the original john
reference.
Question 3: Explain the difference between modifying an object’s contents vs. reassigning a reference.
Modifying an object’s contents means changing the data inside the same object (like updating p.age
).
Both references to that object will see the change.
Reassigning a reference means making the variable point to a completely new object. This only changes the reference locally and does not affect other variables pointing to the original object.
Homework Hack 1
public class ObjectCreation {
public static void main(String[] args) {
Car car1 = new Car("Toyota", 2020);
Car car2 = new Car("Honda", 2018);
System.out.println("Car 1");
car1.fetchInfo();
System.out.println("");
System.out.println("Car 2");
car2.fetchInfo();
}
}
class Car {
public String model;
public int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
public void fetchInfo() {
System.out.println("Model: " + this.model);
System.out.println("Year: " + this.year);
}
}
ObjectCreation.main(null);
Car 1
Model: Toyota
Year: 2020
Car 2
Model: Honda
Year: 2018
Homework Hack 2
public class HeapVsStack {
public static void main(String[] args) {
int pages = 256;
int copiedPages = pages;
Book book1 = new Book("Marvin's Miraculous Mechanical Museum");
Book book2 = book1;
pages = 16;
book1.title = "Carl goes to school";
System.out.println("Book 1");
System.out.println("Title: " + book1.title);
System.out.println("Pages: " + pages);
System.out.println("");
System.out.println("Book 2");
System.out.println("Title: " + book2.title);
System.out.println("Pages: " + copiedPages);
}
}
class Book {
public String title;
Book(String title) {
this.title = title;
}
public void fetchTitle() {
System.out.println("Current Book Title: " + this.title);
}
}
HeapVsStack.main(null);
Book 1
Title: Carl goes to school
Pages: 16
Book 2
Title: Carl goes to school
Pages: 256