Finite and Infinite Recursion with Examples in Java5 May 2025 | 2 min read Recursion is the process by which a function calls itself, either directly or indirectly, and the associated function is known as a recursive function. Recursion makes it easy to solve some difficulties. Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS, and other issues are examples of the problems that can be solved using the Recursion. Types of Recursions: Two different kinds of recursion can be distinguished based on when they terminate:
Finite Recursion:A finite number of recursive calls is required for the recursion to end, which is known as finite recursion. When a base condition is satisfied, a recursion comes to an end. It makes sure the function ends when its task is completed. Implementation:FileName: FiniteRecursionExample.java Output: 5 4 3 2 1 Complexity Analysis: The time complexity is O(N), and the space complexity of the above code is O(N) Infinite Recursion:When the recursion continues after a finite number of recursive calls, it is known as infinite recursion. The recursion continues indefinitely since the base condition is never satisfied. The program will then keep making recursive calls until a StackOverflowError occurs as an output. Implementation:FileName: InFiniteRecursionExample.java Output: 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 Complexity Analysis: The time complexity is non-finite since the recursion will never end, and the space complexity of the above code is non-finite. Next TopicAdvantages of Python over Java |
The distinct() operation in Java 8 is mostly related to streams and is employed to remove duplicate elements from the stream. The Stream API, which was unveiled in Java 8 to offer a more practical method of interacting with collections, includes this action. Syntax Stream<T> distinct() Purpose of distinct()...
6 min read
Java provides the File class for representing a file or directory in the system. The File class is available in java.io package. In order to perform operations on files or directories, the File class provide several useful methods. The delete() method of the File class is one...
3 min read
In Java, method chaining is the chain of methods being called one after another. It is the same as constructor chaining but the only difference is of method and constructor. In this section, we will discuss the method chaining in Java. Method Chaining Method chaining is a common...
2 min read
In Java the Collectors.ToCollection() approach is a maximum beneficial application provided by means of the java.util.Stream.Collectors class which is going to allows you to collect elements from a circulation into a specific type of collection that you specify. The approach provides flexibility in choosing the sort...
3 min read
Concurrency in programming involves multiple threads executing in parallel, which can significantly improve the performance of an application. However, managing concurrent execution can lead to complex problems, such as race conditions, where multiple threads attempt to modify the same variable simultaneously, resulting in unpredictable behavior. Java...
5 min read
In Java, memory management and garbage collection are crucial aspects of maintaining optimal performance and enting memory leaks. One interesting concept related to Java's garbage collection mechanism is the Island of Isolation. The term refers to a group of objects that reference each other but are...
4 min read
Problem Statement Finding an index in an array such that the value at that index appears at more than half of the array's indices is the task at hand. This number is frequently referred to as the array's majority element. An element that occurs more than n/2 times,...
5 min read
Difference Between Java and Core Java Java is a programming language known for its simplicity, object-oriented nature, and platform independence. It consists of three major editions: Java Standard Edition (JSE), Java Enterprise Edition (JEE), and Java Micro Edition (JME). On the other hand, Core Java specifically...
5 min read
A SortedSet's addAll() method preserves the set's uniqueness and order by adding every element from a given collection. All of the elements from the specified collection are appended to the current set using the addAll(Collection C) function. There is no set order in which the elements...
3 min read
A thread is a program in execution created to perform a specific task. Life cycle of a Java thread starts with its birth and ends on its death. The start() method of the Thread class is used to initiate the execution of a thread and it goes...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India