Cosmic Module

J

Qubits of DPK

March 14, 2026

Core Java

Layman Explanation

A method is like a recipe. You name it "Make Coffee", define the steps once, and call it whenever you want coffee. No need to repeat steps every time.

Anatomy of a Method

java
QUBITS OF DPK
1returnType methodName(paramType paramName) {
2    // body
3    return value;   // only if not void
4}

Types of Methods

void Method

java
QUBITS OF DPK
1void greet() {
2    System.out.println("Hello!");
3}

Return Type Method

java
QUBITS OF DPK
1int add(int a, int b) {
2    return a + b;
3}

Static Method

java
QUBITS OF DPK
1static int square(int n) { return n * n; }
2MathHelper.square(5);  // no object needed

Method Overloading

Same name, different parameters — resolved at compile time.
java
QUBITS OF DPK
1int add(int a, int b)           { return a + b; }
2double add(double a, double b)  { return a + b; }
3int add(int a, int b, int c)    { return a + b + c; }
4
5calc.add(10, 20)          // → first
6calc.add(1.5, 2.5)        // → second
7calc.add(10, 20, 30)      // → third

Valid vs Invalid Overloading

java
QUBITS OF DPK
1int add(int a, int b)      // ✅
2int add(int a, int b, int c)  // ✅ different param count
3double add(int a, int b)   // ❌ return type alone NOT valid!

Stack Memory for Methods

javascript
QUBITS OF DPK
1Every method call → Stack Frame created
2Method returns    → Stack Frame DESTROYED
3Local variables die when method ends

Pass By Value — Java's Golden Rule

java
QUBITS OF DPK
1// Primitive — copy passed
2void change(int x) { x = 100; }
3int val = 5;
4change(val);
5System.out.println(val);  // 5 — unchanged!
6
7// Object — copy of reference passed
8void update(Car c) { c.speed = 100; }
9Car car = new Car();
10update(car);
11System.out.println(car.speed);  // 100 — object modified!
Java is always pass-by-value. For objects, the value IS the reference.

Recursion

java
QUBITS OF DPK
1int factorial(int n) {
2    if (n == 0) return 1;           // BASE CASE mandatory!
3    return n * factorial(n - 1);
4}
5// Without base case → StackOverflowError!

️ All Traps

java
QUBITS OF DPK
1// Trap 1 — Return type alone can't overload
2int process(int a) { }
3double process(int a) { }   // ❌
4
5// Trap 2 — Primitives unchanged after method
6void change(int x) { x = 100; }  // original untouched
7
8// Trap 3 — Missing return
9int add(int a, int b) { int s = a+b; }  // ❌ no return!
10
11// Trap 4 — Unreachable code
12return a + b;
13System.out.println("done");  // ❌ unreachable!
14
15// Trap 5 — Recursion without base case = StackOverflowError
16
17// Trap 6 — static can't access instance
18static void show() {
19    System.out.println(instanceVar);  // ❌
20}

Interview Answer (SDE-2)

"A method is a named reusable code block. Java methods have return type, name, parameters, and body. Overloading allows same name with different parameters — resolved at compile time (compile-time polymorphism). Java is always pass-by-value — primitives pass a copy, objects pass a copy of the reference so the object itself can be modified. Every method call creates a stack frame destroyed on return. Recursion needs a mandatory base case to prevent StackOverflowError."

Interview Questions & MAANG-Level Answers

Q1. What is method overloading? How is it different from overriding?
Overloading: same method name, different parameter list (type/count/order), in the SAME class — resolved at compile time (static polymorphism). Overriding: child class redefines parent's method with SAME name and SAME parameters — resolved at runtime (dynamic polymorphism). Return type alone cannot create overloading. @Override annotation should always be used for overriding to catch typos at compile time.
java
QUBITS OF DPK
1// Overloading
2int add(int a, int b) { return a+b; }
3double add(double a, double b) { return a+b; }
4// Overriding
5class Dog extends Animal { @Override void sound() { System.out.println("Woof"); } }
Q2. Is Java pass-by-value or pass-by-reference?
Java is ALWAYS pass-by-value — but the value being passed for objects IS the reference (address). For primitives: a copy of the value is passed — changes inside method don't affect original. For objects: a copy of the reference is passed — both caller and callee point to same heap object, so field modifications are visible, but reassigning the reference inside the method doesn't affect the original. There is no true pass-by-reference in Java.
Q3. What is a stack frame?
Every method call creates a stack frame pushed onto the call stack. The frame contains: local variables, method parameters, return address, and intermediate results. When the method returns, its frame is popped and destroyed — local variables are gone. This is why local variables can't be accessed after method returns. Stack overflow occurs when too many frames are pushed (deep recursion without base case).
Q4. What is the difference between void and a return type method?
void methods perform an action but return nothing — used for operations like printing, updating state, sending notifications. Return type methods compute and return a value — caller can use the result. void methods can have an empty return; to exit early (not to return a value). You cannot assign the result of a void method to a variable.
Q5. Why must recursive methods have a base case?
Without a base case, the method calls itself infinitely, creating new stack frames continuously until the JVM's call stack overflows, throwing StackOverflowError. The base case is the termination condition that stops recursion. Example: factorial(0) return 1 is the base case that stops factorial(n) = n * factorial(n-1). Every recursive problem must have at least one base case and each recursive call must move toward it.