Methods and Recursion in JAVA

JAVA PROGRAMS ON METHODS AND RECURSION

Methods in JAVA must be used when we need to execute some task may times in a program, so a method must be written and must be called whenever that task is to be executed, thus saving us to write the same code again and again, thereby providing code reusability.

For example, let us say we need to calculate the permutation(nPr) and combination(nCr), then for both these calculations we would require first to calculate the factorial. Now instead of writing factorial code for nPr and then agian for nCr, it would save us time by creating a method to calculated factorial and then using it by just calling the method name, thus saving time from writing the same code again and also improves code readability.

Before you go on to programs just have a look at different ways of calling methods in java.

As for recursion, it is nothing but a method calling itself. It is a very powerful method for solving complex problems, as sometimes coding these complex problems using iterative approach might be a herculean task.You will get a more clear picture once you dive into data structures as you will notice that coming up with a recursive solution is much easier than coming up with an iterative one for the same problem.

Share Me!
  •  
  •  
  •  
  •  
  •  
  •