Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

March 11, 2015 Categories: Core Java Basics. No Comments on Recursion in Java

This article explains about recursion in Java. We will learn what is recursion and how to use recursion to write effective Java programs.

Recursion is a famous way to solve problems with less lines of code. Many programmers prefer recursion over iteration as less amount of code is required to solve problems using recursion.

A method calling itself in its definition (body) is known as recursion. For implementing recursion, we need to follow the below requirements:

  • There should be a base case where the recursion terminates and returns a value.
  • The value of the parameter(s) should change in the recursive call. Otherwise, this leads to infinite loop.

Let’s consider the code for finding factorial of a number using iteration as shown below:

Now, let’s consider the recursive solution for finding the factorial of a given number which is shown below:

The complete Java program which demonstrates recursion is given below:

 

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me