Recursion Program in Java

Amansingh Javatpoint
1 min readFeb 23, 2021

Recursion in java is a process in which a method calls itself continuously. A method in java that calls itself is called recursive method.

It makes the code compact but complex to understand.

Syntax:

  1. returntype methodname(){
  2. //code to be executed
  3. methodname();//calling same method
  4. }

Java Recursion Example 1: Infinite times

  1. public class RecursionExample1 {
  2. static void p(){
  3. System.out.println(“hello”);
  4. p();
  5. }
  6. public static void main(String[] args) {
  7. p();
  8. }
  9. }

Output:

hello
hello
...
java.lang.StackOverflowError

--

--