+ 1

Why is the output 3.

I was expecting 1 as the output. Can somebody explain. https://code.sololearn.com/ck4rd1RzFpo7/?ref=app

29th Aug 2020, 4:42 PM
stephen haokip
stephen haokip - avatar
2 Réponses
+ 2
First one is 1) main method then 2) getStackTraceDepth() 3) getStackTrace()
29th Aug 2020, 5:07 PM
Jayakrishna 🇮🇳
+ 2
You can add for(StackTraceElement st:ste) { System.out.println(st); } To your code then you will get elements of stack trace. like... java.lang.Thread.getStackTrace() ---->stack element 3(top element) InTheBlueDepthOfStackTrace.getStackTraceDepth() ----->stack element 2 InTheBlueDepthOfStackTrace.main() ----->stack element 1(bottom element) In your code,the getStackTrace() of Thread class method returns array of stack trace elements representing the stack dump of the thread. The first element of an array represents the top of the stack which is the last method invocation in the sequence. The last element of the array represents the bottom of the stack which is the first method invocation in the sequence. So,that's why your output is ' 3 '. https://code.sololearn.com/c6TEfr9jI0gu/?ref=app
30th Aug 2020, 6:27 AM
NavyaSri
NavyaSri - avatar