Java Java recursion longest common subsequence

Click For Summary
Java recursion operates similarly to recursion in other programming languages, encompassing both functional and type recursion. Functional recursion involves a function calling itself, exemplified by the factorial function, which includes a base case and a recursive case. Type recursion is illustrated through data structures like linked lists, where a type is defined in terms of itself. However, Java has specific limitations; the Java Virtual Machine (JVM) does not efficiently support tail recursion, making recursive algorithms susceptible to stack overflow errors due to memory exhaustion. This issue is also present in other JVM languages, such as Scala and Clojure, where recursion is commonly used. Understanding these nuances is crucial for effectively implementing recursion in Java.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 39 ·
2
Replies
39
Views
7K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K