Java Recursion — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 23 / 50 · தமிழ்

Java Recursion

Solve a smaller version of the same problem.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.

இந்தப் பாடத்தில்…

  • Explain the key idea behind Recursion.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

A recursive method calls itself with a smaller or simpler input. Define the base case first, then explain why each call moves toward it. Recursion consumes stack space; a mathematically elegant solution is not always the best production implementation.

02 / Step by step

விரிவான விளக்கங்கள்

Recursion: Function தன்னை தானே அழைக்கும்

Recursion என்பது ஒரு method தன்னையே call பண்றது. Complex problems-ஐ சுலபமா solve பண்றதுக்கு use ஆகுது.

Recursion-க்கு ஒரு base case இருக்கணும் — இல்லன்னா infinite recursion ஆகும்.

Recursion Example — Factorial

5! = 5 × 4 × 3 × 2 × 1 = 120 — Recursion-ல எப்படி calculate பண்றதுன்னு பாரு:

Recursion elegant-ஆ இருக்கும், ஆனா careful-ஆ use பண்ணணும். Base case இல்லன்னா StackOverflowError வரும்!

Fibonacci Sequence with Recursion

Java reference fragmentJAVA
static int fibonacci(int n) {
    if (n <= 1) return n;  // base cases
    return fibonacci(n - 1) + fibonacci(n - 2);
}

for (int i = 0; i < 8; i++) {
    System.out.print(fibonacci(i) + " ");
}
// Output: 0 1 1 2 3 5 8 13

Golden Rules of Recursion

  1. Always have a base case — the condition that stops recursion
  2. Move toward the base case — every recursive call must bring you closer to stopping
  3. Trust the recursion — assume the recursive call correctly solves the smaller sub-problem
  4. Watch for stack overflow — deeply nested calls consume memory

Recursion-ல் 'base case' ஏன் அவசியம்?

  1. Code faster-ஆக run ஆக
  2. Infinite recursion மற்றும் StackOverflowError-ஐ தடுக்க
  3. Memory save செய்ய
  4. Better readability-க்கு
Reveal answer / விடை

Infinite recursion மற்றும் StackOverflowError-ஐ தடுக்க

Base case இல்லாமல் function தன்னை infinite-ஆக call செய்யும் — StackOverflowError வரும். Base case என்பது recursion நிறுத்தும் condition.

03 / Read and predict

முழுமையான உதாரணம்

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Sums.upTo(4));
    }
}
class Sums {
    static int upTo(int n) {
        if (n <= 0) return 0;
        return n + upTo(n - 1);
    }
}
Predict first. Then reveal the expected output / வெளியீடு
10

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

Practice challenge

Trace the calls for upTo(3) by hand before running them.

Reveal the answer and reasoning / விடை

The calls reach 3 + 2 + 1 + 0, so the result is 6.

தவிர்க்க வேண்டிய தவறு: Without a reachable base case, recursion can end in StackOverflowError. Large inputs can also overflow the numeric result.

பாடத்தின் சுருக்கம்

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.