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 |
Java is a versatile and popular programming language known for its object-oriented nature. In Java, everything is an object, and objects are instances of classes. While working with classes, we may encounter the concept of instance blocks, also known as instance initialization blocks. In this section,...
6 min read
Pattern-based programming is a fascinating aspect of coding that allows developers to create visually appealing designs using characters and symbols. In Java, creating alternate patterns can be a stimulating exercise that not only hones your programming skills but also enhances your understanding of loops and control...
5 min read
The Mars Rover problem is a classic programming challenge that tests one's ability to design an algorithm to navigate a rover on a rectangular grid. The goal is to manoeuvre the rover based on a set of commands, avoiding obstacles and staying within the bounds of...
6 min read
In programming, indentation is just like formatting. It is used to make the code readable to other users because it makes the code easier to edit, displays how the braces match up, and shows the logic of the program in well-organized fashion. It signals to the...
4 min read
In the context of Reactor and the Spring ecosystem, a Mono is a fundamental building block for reactive programming. It represents a stream of zero or one element and is part of Project Reactor that provides a foundation for building reactive applications on the Java Virtual...
3 min read
In the given array, the task is to find the kth smallest element of the array, where k is always less than the size of the given array. Examples: Input: arr[] = {56, 34, 7, 9, 0, 48, 41, 8} k = 3 Output: The 3rd smallest element of the array...
11 min read
In the realm of object-oriented programming, immutability is a powerful concept that enhances code robustness, thread safety, and overall program stability. An immutable class is one whose instances cannot be modified after creation. While immutability brings numerous advantages, there are scenarios where we need to handle...
7 min read
The java.nio.The FloatBuffer class has a duplicate() function. To create a new float buffer that shares the contents of the supplied buffer, use the FloatBuffer Class. This buffer's contents will make up the new buffer. The new buffer will reflect changes made to this buffer's...
4 min read
Deserialization, commonly called unmarshalling, returns serialized data to its original form so that an application may utilize it. Through the Serializable and Externalizable interfaces, which are used to turn objects into streams of bytes and back again, Java supports serialization and deserialization. Custom Serialisation By implementing the Serializable...
4 min read
Java is a widely-used, versatile programming language known for its portability and reliability. However, like any programming language, it can throw errors that may seem cryptic to newcomers. One such error is the "Invalid Target Release: 9" error. In this section, we will explore the meaning...
6 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