public static void main (String[] args)
{
System.out.println(factorial(5));
}
public int factorial(int n)
{
if(n <= 1){
return 1;
}
else{
A practical approach which requires a good IDE (eclipse, netbeans, IntelliJ):
Add a breakpoint to the line which reads return 1 and debug the application. When it stops, look at the stack trace. You'll see that the factorial method has been called several times.
The eclipse Debug view shows the suspended thread and a stack with six entries, each line representing a line of code where another method is called (except for the top entry - that's the breakpoint). factorial appears five times, you can select each line and see the value of n in the Variable view (this is basic and should work on the other IDE in a similiar way).
That should give another idea how recursive method calls work (and why you receive an out of memory error when you do not define the exit criteria properly ;) )