{"id":1198,"date":"2022-02-25T16:41:39","date_gmt":"2022-02-25T16:41:39","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1198"},"modified":"2022-02-25T16:41:39","modified_gmt":"2022-02-25T16:41:39","slug":"java-recursion","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-recursion\/","title":{"rendered":"Java Recursion"},"content":{"rendered":"\n<p>In this tutorial, you will learn about Java recursive function, its advantages and disadvantages.<\/p>\n\n\n\n<p id=\"introduction\">In Java, a\u00a0method\u00a0that calls itself is known as a recursive method. And, this process is known as recursion.<\/p>\n\n\n\n<p>A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-works\">How Recursion works?<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-recursive-call.jpg\" alt=\"A function is calling itself\" title=\"Working of Java Recursion\"\/><figcaption>Working of Java Recursion<\/figcaption><\/figure>\n\n\n\n<p>In the above example, we have called the&nbsp;<code>recurse()<\/code>&nbsp;method from inside the&nbsp;<code>main<\/code>&nbsp;method. (normal method call). And, inside the recurse() method, we are again calling the same recurse method. This is a recursive call.<\/p>\n\n\n\n<p>In order to stop the recursive call, we need to provide some conditions inside the method. Otherwise, the method will be called infinitely.<\/p>\n\n\n\n<p>Hence, we use the\u00a0if&#8230;else statement\u00a0(or similar approach) to terminate the recursive call inside the method.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example: Factorial of a Number Using Recursion<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Factorial {\n\n    static int factorial( int n ) {\n        if (n != 0)  \/\/ termination condition\n            return n * factorial(n-1); \/\/ recursive call\n        else\n            return 1;\n    }\n\n    public static void main(String&#91;] args) {\n        int number = 4, result;\n        result = factorial(number);\n        System.out.println(number + \" factorial = \" + result);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>4 factorial = 24<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have a method named&nbsp;<code>factorial()<\/code>. The&nbsp;<code>factorial()<\/code>&nbsp;is called from the&nbsp;<code>main()<\/code>&nbsp;method. with the&nbsp;<var>number<\/var>&nbsp;variable passed as an argument.<\/p>\n\n\n\n<p>Here, notice the statement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return n * factorial(n-1);<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>factorial()<\/code>&nbsp;method is calling itself. Initially, the value of n is 4 inside&nbsp;<code>factorial()<\/code>. During the next recursive call, 3 is passed to the&nbsp;<code>factorial()<\/code>&nbsp;method. This process continues until&nbsp;<var>n<\/var>&nbsp;is equal to 0.<\/p>\n\n\n\n<p>When&nbsp;<var>n<\/var>&nbsp;is equal to 0, the&nbsp;<code>if<\/code>&nbsp;statement returns false hence 1 is returned. Finally, the accumulated result is passed to the&nbsp;<code>main()<\/code>&nbsp;method.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"factorial-program\">Working of Factorial Program<\/h2>\n\n\n\n<p>The image below will give you a better idea of how the factorial program is executed using recursion.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/how-recursion-works-java.jpg\" alt=\"Finding the factorial of a number using recursion\" title=\"Factorial Program using Recursion\"\/><figcaption>Factorial Program using Recursion<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-disadvantages\">Advantages and Disadvantages of Recursion<\/h2>\n\n\n\n<p>When a recursive call is made, new storage locations for variables are allocated on the stack. As, each recursive call returns, the old variables and parameters are removed from the stack. Hence, recursion generally uses more memory and is generally slow.<\/p>\n\n\n\n<p>On the other hand, a recursive solution is much simpler and takes less time to write, debug and maintain.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about Java recursive function, its advantages and disadvantages. In Java, a\u00a0method\u00a0that calls itself is known as a recursive method. And, this process is known as recursion. A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[484],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1198"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1198"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1198\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}