-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackVsHeapExample.java
More file actions
125 lines (94 loc) · 3.38 KB
/
Copy pathStackVsHeapExample.java
File metadata and controls
125 lines (94 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Day 7 - Memory Basics: Stack vs Heap
*
* Concept:
* Java uses two types of memory:
* 1. Stack - for primitives and method calls (fast, limited)
* 2. Heap - for objects (slower, large, automatic cleanup)
*
* This example demonstrates:
* - How primitives are stored on stack
* - How objects are stored on heap with references on stack
* - Difference between primitive and reference assignment
*
* Real-life analogy:
* Stack = Quick, organized checkout counter
* Heap = Slower, large storage warehouse
*/
public class StackVsHeapExample {
/**
* Main method - entry point of the program
*/
public static void main(String[] args) {
System.out.println("--- Primitives (Stack Storage) ---");
// Primitives stored on stack
int age = 25;
int salary = 100000;
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
// Modifying primitives
int temp = age;
age = salary;
salary = temp;
System.out.println("After swap:");
System.out.println("Age: " + age); // 100000
System.out.println("Salary: " + salary); // 25
// Key: Primitives are INDEPENDENT
System.out.println("\n--- Objects (Heap Storage) ---");
// Objects stored on heap, references on stack
Student s1 = new Student("Tejas", 20);
Student s2 = s1; // s2 gets same reference
System.out.println("s1: " + s1.name + ", Age: " + s1.age);
System.out.println("s2: " + s2.name + ", Age: " + s2.age);
// Modifying through s1
s1.name = "Raja";
System.out.println("\nAfter s1.name = 'Raja':");
System.out.println("s1: " + s1.name); // "Raja"
System.out.println("s2: " + s2.name); // "Raja" (same object)
// Key: Both references point to SAME object
System.out.println("\n--- Creating New Object ---");
// s2 now points to different object
s2 = new Student("Priya", 19);
System.out.println("s1: " + s1.name + ", Age: " + s1.age); // "Raja"
System.out.println("s2: " + s2.name + ", Age: " + s2.age); // "Priya"
// Now s1 and s2 point to DIFFERENT objects
System.out.println("\n--- Mixed Primitive and Reference ---");
demonstrateMemory();
}
/**
* Method to demonstrate stack and heap memory usage
*/
public static void demonstrateMemory() {
// Variables on stack
int price = 50000; // Primitive on stack
String carModel = "Tesla"; // Reference on stack, String on heap
Car car = new Car("Red"); // Reference on stack, Car object on heap
System.out.println("Inside method:");
System.out.println("Price: " + price);
System.out.println("Model: " + carModel);
System.out.println("Car color: " + car.color);
// After method ends:
// - All stack variables freed
// - Heap objects freed when no references exist
}
/**
* Inner class to represent Student
*/
static class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
/**
* Inner class to represent Car
*/
static class Car {
String color;
Car(String color) {
this.color = color;
}
}
}