Skip to main content

Recursive Programming in Java

Posted by mortazavi on August 3, 2005 at 8:18 PM PDT

I've done a lot of recursive programming in Java.

I like recursion. That's my weakness. Why? I don't know. Perhaps because I spent a good many years of my life studying logic and methodology of science at Berkeley. There's just a certain beauty to it. So, after many years of logic, when I took a course in LISP, I grew an admiration for it. However, that didn't stop me there. I moved to implement much of CORBA's Relationship Service some 8 years ago in Java, using a lot of recursion. In implementing parts of the POA policy system some 6 years ago here at Sun, I used recursion again.

Here's a snippet of code from Lewis & Chase's book on programming in Java which shows recursion in a mischievous but fun manner in their TowerOfHanoi.java.


   private void moveTower 
   (int numDisks, int start, int end, int temp)
   {
      if (numDisks == 1)
         moveOneDisk (start, end);
      else
      {
         moveTower (numDisks-1, start, temp, end);
         moveOneDisk (start, end);
         moveTower (numDisks-1, temp, end, start);
      }
   }

So, revel in the power of recursion.

It is not always easy to deploy it but when it is, take advantage of it.

Related Topics >>