Recursion Program in Java
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:
- returntype methodname(){
- //code to be executed
- methodname();//calling same method
- }
Java Recursion Example 1: Infinite times
- public class RecursionExample1 {
- static void p(){
- System.out.println(“hello”);
- p();
- }
- public static void main(String[] args) {
- p();
- }
- }
Output:
hello
hello
...
java.lang.StackOverflowError