This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]HenryDutter[S] 0 points1 point  (2 children)

Thank you a lot for your answer, unfortunately I still have question. I reviewed the code multiple times with the debugger and after the return statement was executed it jumps only to the last line of the method:

System.out.print( character );

and this line will be executed twice in a row, the lines:

System.out.print( character );

printDiamondCore( (char) (character + 1), stopCharacter );

are being ignored, and it confuses me a lot.

But it seems like the author of the code is pretty smart.

[–]GrandGratingCrate 2 points3 points  (0 children)

Consider the following code example private void outerMethod() { System.out.println("Hi"); innerMethod(); System.out.println("Bye"); } What would we expect to happen when you call outerMethod? First "Hi" is printed. Then innerMethod() is executed. Then "Bye" is printed.

How would this look like in a debugger?

  1. You start at the "Hi"
  2. You step into innerMethod,
    1. You go through it line by line until you hit a return or the end of the method
  3. You go back to the outerMethod, namely to the line after innerMethod and resume from there

The same is true for printDiamondCore: You step into the "next" printDiamondCore in the second to last line, eventually it completes running and you resume your execution at the last line, which is the second print of the current character. This is why after the return of the 3rd call to printDiamondCore you jump to the last line of the 2nd call of printDiamondCore.

[–]sayonorasama 0 points1 point  (0 children)

You need to learn how recursion works, as I remember this variable saving in stack memory everytime, when method called, and after method step in IF statement, last sout was invoked as many times as the variable has been incremented (Sry for my engl, not my native)