I got lucky: My favorite elementary school, Mrs. Turing, always wrote this phrase on the blackboard: "The last function called is the first function to end". She made us memorize it and swore that one day we would understand. Thank you, Mrs. Turing. It seems the promised day has come. I can even write code to get a stack trace that is 10 calls deep. Can you?
Stack trace with 10 calls
- 3
Locked
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
joe
14 August 2020, 10:24
method 1 calls :
getStackTrace(), method10() to method1() & main = 12
method 10 calls:
getStackTrace(), method10() & main = 3
+1
Nikolaos
27 August 2019, 16:29
I have solve it also, but i don't underastand it yet. I hope, it will be more cleal in the next lessons.
+1
jonathan D
14 September 2019, 16:35
I think the logic is this:
1- We set the stack trace in method10()
2- We call method1() and method10() from main() in this line:
int stackTraceLength = method1().length - method10().length + 1;
For the method1() call:
when we reach the stack trace in method10() we go back and come across: getStackTrace() -> methods(from 10 to 1) -> main() == 12
For the method10() call:
when we reach the stack trace in method10() we go back and come across: getStackTrace() -> method10() -> main() == 3
and 12 - 3 +1 = 10.
You can check that if you comment the line 10 with the variable stackTraceLength in main() and write:
+6
yehuda b
11 February 2020, 17:42
Thanks! very clear!
0
Khairul
15 May 2019, 09:10
I have solved it. I understand why is the value of method10().length is equal to 12. Because all of the previous method and the method10 and the stackTrace method is in the Stack.
---------------------------------------------------------------------------------------------------------------
But can you tell me, why is the value of method1().length is 21?
0
Ahmed
15 May 2019, 11:08
the value of method1().length is 12 and the value of method10().length is 3
put a breakpoint on the main method and you will see
+2
Khairul
15 May 2019, 12:08
Can you clarify it a little bit more with example?
0
Ahmed
15 May 2019, 12:53
Read about intellij breakpoint and how to debug your code.
+3