Iteration v/s Recursion and Mutable v/s Immutable
Mutable | Immutable | |
---|---|---|
Iteration | S- H- | S- H^ |
Recursion | S^ H- | S^ H^ |
Tail Recursion without Optimization | S^ H- | S^ H ^ |
Tail Call Optimization | S- H- | S- H- * |
* Heap size remains the same assuming Garbage collection takes places regularly.
Here, when I say Immutable, I mean the class in which the method (using iterator or recursive) is contained is Immutable. .i.e. the method is trying to update the instance variables, but they are declared final, hence they cannot be modified. Only way to modify them is to create a new object with the updated instance variables.
Legend:
S | Stack Memory Size |
H | Heap Memory Size |
– | Remains the same |
^ | Increases |
For Ex: S^ means Stack size grows.