Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java recursion longest common subsequence

  1. Apr 13, 2009 #1
    can someone explain java recursion to me?
     
    Last edited: Apr 13, 2009
  2. jcsd
  3. Apr 13, 2009 #2
    I believe that recursion in Java works about the same as it does in most languages.

    Basically, you have functional recursion and type recursion. Type recursion would be like a linked list. Functional recursion would be like a function calling itself. For instance...

    Code (Text):

    class LinkedNode
    {
       public int data;
       public LinkedNode next;
    }
     
    LinkedNode is a recursively defined type:
    LinkedNode : null | int & LinkedNode.

    For functions...

    Code (Text):

    int factorial(int n)
    {
       if(n<=0)
          return 1;
       else
          return n * factorial(n-1);
    }
     
    This is saying that factorial is 1 if n<=0, and n * factorial(n-1) otherwise (n > 0).

    Basically, recursive definitions need a base case (null, n<=0) and a recursive part (some new information PLUS some (usually smaller) instance of the very same definition).
     
  4. Apr 25, 2009 #3
    Good insights, but Java has special concerns. Be warned that the Java Virtual Machine does not support tail recursion well. Consequently, recursive algorithms are more prone to "blow up the stack" by exhausting memory. This problem plagues all JVM languages, including Scala and Clojure, where recursion is heavily relied on.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java recursion longest common subsequence
  1. Recursion in C (Replies: 12)

  2. [Java] PNGException (Replies: 2)

  3. JAVA GridLayout (Replies: 1)

  4. Java interpreter (Replies: 8)

  5. Java Cylinders (Replies: 2)

Loading...