
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the differences between recursion and iteration in Java?
The Recursion and Iteration both repeatedly execute the set of instructions. Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that recursion is a process, always applied to a function and iteration is applied to the set of instructions which we want to get repeatedly executed.
Recursion
- Recursion uses selection structure.
- Infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on some condition (base case) and Infinite recursion can crash the system.
- Recursion terminates when a base case is recognized.
- Recursion is usually slower than iteration due to the overhead of maintaining the stack.
- Recursion uses more memory than iteration.
- Recursion makes the code smaller.
Example
public class RecursionExample { public static void main(String args[]) { RecursionExample re = new RecursionExample(); int result = re.factorial(4); System.out.println("Result:" + result); } public int factorial(int n) { if (n==0) { return 1; } else { return n*factorial(n-1); } } }
Output
Result:24
Iteration
- Iteration uses repetition structure.
- An infinite loop occurs with iteration if the loop condition test never becomes false and Infinite looping uses CPU cycles repeatedly.
- An iteration terminates when the loop condition fails.
- An iteration does not use the stack so it's faster than recursion.
- Iteration consumes less memory.
- Iteration makes the code longer.
Example
public class IterationExample { public static void main(String args[]) { for(int i = 1; i <= 5; i++) { System.out.println(i + " "); } } }
Output
1 2 3 4 5
- Related Articles
- Difference Between Recursion and Iteration
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between Java classes and Java objects?
- What are the differences between GridLayout and GridBagLayout in Java?
- What are the differences between JTextField and JTextArea in Java?
- What are the differences between JRadioButton and JCheckBox in Java?
- What are the differences between StackOverflowError and OutOfMemoryError in Java?
- What are the differences between the TableCellRenderer and TableCellEditor in Java?
- What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?\n
- What are the differences between length and length () in Java?\n
- What are the differences between JFrame and JDialog in Java?\n
- What are the differences between a class and an interface in Java?
- What are the differences between default constructor and parameterized constructor in Java?
- What are the differences between tight coupling and loose coupling in Java?

Advertisements