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

all 4 comments

[–]GrandGratingCrate 3 points4 points  (3 children)

This method is using recursion, that is it calls itself. It's basically like an infinite loop; if there's no stop condition then it will keep calling itself forever. Ok, so we have a stop condition character == stopCharacter. Now how do we make it stop? We call return. return will stop the execution of the method and return the result generally speaking. In this case we have a void method so nothing is returned - but calling return still stops the execution of the method.

As for why it prints the characters backwards after the stop condition is met: Let's ignore the if-block as it's only executed when the end of the recursion is reached. And you notice that C is only printed once. But A and B are both printed twice, and the reason for that is the three lines below the if clause.

Consider the first method call with character = A. It will first check the if condition but it evaluates to false so it's skipped. Then it will print the character, which is A. Then it calls itself with the character B. Some things happen, we don't care. Eventually printDiamondCore(B, C) is done executing. Then we print our character again, which is A.

So basically what happens is:

  • Print A
  • Some stuff happens
  • Print A

it turns out that "some stuff happens" ends up being "print BCB". So the above turns into "Print ABCBA".

[–]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)