Java recursion longest common subsequence

Click For Summary
SUMMARY

The discussion focuses on Java recursion, highlighting two main types: functional recursion and type recursion. Functional recursion is exemplified by the factorial function, which demonstrates the need for a base case and a recursive case. The discussion emphasizes that while recursion is a common programming concept, Java's Virtual Machine (JVM) does not efficiently support tail recursion, leading to potential stack overflow issues. This limitation is also present in other JVM languages such as Scala and Clojure.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with recursion concepts
  • Knowledge of data structures, specifically linked lists
  • Awareness of Java Virtual Machine (JVM) behavior
NEXT STEPS
  • Study Java recursion patterns in depth
  • Learn about stack overflow prevention techniques in Java
  • Explore alternatives to recursion, such as iterative solutions
  • Investigate the performance implications of recursion in JVM languages
USEFUL FOR

Software developers, computer science students, and anyone interested in mastering recursion in Java and understanding its limitations within the JVM context.

physicsfun
Messages
10
Reaction score
0
can someone explain java recursion to me?
 
Last edited:
Technology news on Phys.org
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:
class LinkedNode
{
   public int data;
   public LinkedNode next;
}

LinkedNode is a recursively defined type:
LinkedNode : null | int & LinkedNode.

For functions...

Code:
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).
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 39 ·
2
Replies
39
Views
8K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K