Casting HW
For the thinkers group. 1.13.
CSA Unit 1 – Object Creation and Storage
Popcorn Hack #1 – Stack vs Heap
public class MemoryExample {
public static void main(String[] args) {
// Stack: primitives store actual values
int x = 10;
int y = x;
y = 20;
System.out.println("Stack values:");
System.out.println("x = " + x + ", y = " + y);
// Heap: objects store references
int[] nums1 = {1, 2, 3};
int[] nums2 = nums1;
nums2[0] = 99;
System.out.println("\nHeap values:");
System.out.println("nums1[0] = " + nums1[0]);
System.out.println("nums2[0] = " + nums2[0]);
}
}
class NumberHolder {
int value;
NumberHolder(int value) {
this.value = value;
}
}
public class PassByReferenceTest {
public static void modify(NumberHolder n) {
n.value += 5;
System.out.println("Inside modify(): value = " + n.value);
}
public static void main(String[] args) {
NumberHolder num = new NumberHolder(10);
System.out.println("Before: " + num.value);
modify(num);
System.out.println("After: " + num.value);
}
}
public class MemoryDemo {
public static void main(String[] args) {
// Stack primitives
int a = 10;
int b = a;
b = 20;
System.out.println("a = " + a + ", b = " + b);
// Heap objects
int[] array1 = {1, 2, 3};
int[] array2 = array1;
array2[0] = 99;
System.out.println("array1[0] = " + array1[0] + ", array2[0] = " + array2[0]);
}
}
Questions (answer in notebook):
-
Why does changing
bnot affecta, but changingarray2affectsarray1? -
Describe what’s on the stack vs. the heap.
public class IntByValue {
public static void changeInt(int n) {
n = n + 10;
System.out.println("Inside method: n = " + n);
}
public static void main(String[] args) {
int n = 5;
System.out.println("Before method: n = " + n);
changeInt(n);
System.out.println("After method: n = " + n);
}
}
class NumberHolder {
int value;
NumberHolder(int value) {
this.value = value;
}
}
public class PassByReferenceDemo {
public static void changeValue(NumberHolder n) {
n.value += 10;
}
public static void main(String[] args) {
NumberHolder myNumber = new NumberHolder(5);
System.out.println("Before: " + myNumber.value);
changeValue(myNumber);
System.out.println("After: " + myNumber.value);
}
}
public class PersonDemo {
static class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public static void haveBirthday(Person p) {
p.age++;
System.out.println("Inside method: " + p.name + " is now " + p.age);
}
public static void reassignPerson(Person p) {
p = new Person("New Person", 99);
System.out.println("Inside reassign(): " + p.name + " is " + p.age);
}
public static void main(String[] args) {
Person john = new Person("John", 20);
System.out.println("Before birthday: " + john.name + " is " + john.age);
haveBirthday(john);
System.out.println("After birthday: " + john.name + " is " + john.age);
System.out.println("\nBefore reassign: " + john.name + " is " + john.age);
reassignPerson(john);
System.out.println("After reassign: " + john.name + " is " + john.age);
}
}
public class Student {
private String name;
private int grade;
private double gpa;
// Default constructor
public Student() {
this.name = "Nora";
this.grade = 11;
this.gpa = 5.0;
}
// Parameterized constructor
public Student(String name, int grade, double gpa) {
this.name = name;
this.grade = grade;
this.gpa = gpa;
}
public void display() {
System.out.println("Name: " + name + ", Grade: " + grade + ", GPA: " + gpa);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Soni", 11, 1.0);
s1.display();
s2.display();
}
}
public class HeapVsStack {
public static void main(String[] args) {
// 1. Create a primitive variable (int pages)
int pages = 100;
// 2. Create another primitive variable that copies it
int copiedPages = pages;
// 3. Create a Book object
Book b1 = new Book("Java Basics");
// 4. Create another Book reference that points to the same object
Book b2 = b1;
// 5. Change the original primitive and the Book title
pages = 200;
b1.title = "Advanced Java";
// 6. Print both sets of values to compare behavior
System.out.println("Primitive values:");
System.out.println("pages = " + pages);
System.out.println("copiedPages = " + copiedPages);
System.out.println("\nObject references:");
System.out.println("b1 = " + b1);
System.out.println("b2 = " + b2);
}
}
class Book {
// 1. Declare variable
String title;
// 2. Constructor
public Book(String title) {
this.title = title;
}
// 3. toString() method
public String toString() {
return "Book title: " + title;
}
}
```Primitive values: pages = 200 copiedPages = 100
Object references: b1 = Book title: Advanced Java b2 = Book title: Advanced Java