# Can't understand simple Recursion:

• December 7th, 2013, 09:47 PM
TSSF44
Can't understand simple Recursion:
Code :

```  class RecursionTest { public static void main(String[] args) {   System.out.println(reverse("ABCDEF")); } //---------------------------------- public static String reverse(String z) { if (z.length() <= 1) return z; return reverse(z.substring(1, z.length())) + z.substring(0, 1); } }```

Output:
FEDCBA

I understand that s.substring(1, s.length()) is the tail of the string, BCDEF and s.substring(0,1) is just the first letter , A. But how does this method actually reverse it? Where is the increment/decrement located?

The first time around passes the method BCDEF and concatenates A on the end.... then what?

Edit: maybe it works something like this? :

first time passing it- BCDEF is tail, keep A to the side because its last ---> BCDEF A

now you get CDEF and take B from it, put it before A on the side --------> CDEF BA

now its DEF.... put c next to b ------------------------------------------> DEF CBA

EF, put D next to C------------------------------------------------> EF DCBA

F--------------------------------------------------------------------> F EDCBA

= FEDCBA
• December 9th, 2013, 05:11 PM
jps
Re: Can't understand simple Recursion:
First call: ABCDEF > 1, so return a recursive call with the first character appended to the end. Which at this point is some undetermined string + A.
Second call: BCDEF > 1, so return a recursive call with the first character appended to the end. Which at this point is some undetermined string + B.
Third call: CDEF > 1, so return a recursive call with the first character appended to the end. Which at this point is some undetermined string + C.
Fourth call: DEF > 1, so return a recursive call with the first character appended to the end. Which at this point is some undetermined string + D.
Fifth call: EF > 1, so return a recursive call with the first character appended to the end. Which at this point is some undetermined string + E.
Sixth call: F = 1, so return F.
This is the turning point where the unknown strings are now known and the recursion starts to unwind.
The string F is returned to the fifth call, which appends E, resulting in FE.
FE is returned to the fourth call, which appends D, resulting in FED.
FED is returned to the third call, which appends C, resulting in FEDC.
FEDC is returned to the second call, which appends B, resulting in FDECB.
FDECB is returned to the first call, which appends A, and finally exits the recursive method returning FEDCBA to where the method was first called.
It is important to note that a recursive method stacks up all of the calls until the return is hit where the method does not call itself again. In this example, when the string was one character long the string F was the first real return value. All other calls were in memory waiting on that F to work it's way back to the start. The first call can not return until the second call does. The second call can not return until the third call does. So on and so on until there is no memory left or the method finally returns a value rather than calling itself again.