+ 1
Why is the output 3.
I was expecting 1 as the output. Can somebody explain. https://code.sololearn.com/ck4rd1RzFpo7/?ref=app
2 Antworten
+ 2
First one is
1) main method then
2) getStackTraceDepth()
3) getStackTrace()
+ 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