Stack trace - 1

"Hi! Today I'll tell you what a stack trace is. But first I need to tell you what a stack is."

"Imagine a pile of papers – directives for a certain employee. You can put a new task on top of the pile, or you can take a task from the top. This means that tasks won't be executed in the order they were received. The task placed on the pile most recently will be the first to be executed. Structuring the elements of a collection this way forms a stack."

"Java has a special collection for that – Stack. It's a collection that has methods to 'add an element' and 'take (get) an element'. As you may have guessed, the element that was added last will be the first to be taken."

"Sounds rather straightforward."

"Great. Now I'll explain what a stack trace is."

"Imagine that in a Java program method A called method B, which called method C, which in turn called method D. To exit method B, we must first exit method C, and to do that – we must first exit method D. This behavior resembles a stack."

"Why do you say it resembles it?"

"To get to some task in the middle of our stack of papers, for example, you first need to execute all the tasks lying on top of it."

"There's some similarity, but I'm not sure I understand everything correctly."

"Look. A stack is a set of elements. Like pieces of paper in a pile. To take the third piece of paper from the top, you first need to take the second, and for that, you need to take the first. You can always put and take pieces of paper, but you always have to take the top paper first."

"The same is true for function calls. Method A calls method B, which calls method C. To exit A, you must first exit B, and to do that, you need to exit C."

"Wait. If I understand what you're saying, the entire concept of a stack boils down to 'take the piece of paper that was added last' and 'you can only exit the method you most recently entered'. Is that accurate?"

"Yes. The sequence of function calls is known as the ‘call stack' or simply the ‘stack'. The last function called is the first function to end. Let's dig into an example."

Get and display the current call stack:
public class ExceptionExample
{
  public static void main(String[] args)
  {
    method1();
  }

  public static void method1()
  {
    method2();
  }

  public static void method2()
  {
    method3();
  }

  public static void method3()
  {
     StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
    for (StackTraceElement element : stackTraceElements)
    {
       System.out.println(element.getMethodName());
    }
  }
}
Result:
getStackTrace
method3
method2
method1
main
undefined
9
Task
New Java Syntax, level 9, lesson 1
Locked
Integer literals
Four public fields, corresponding to the four integer types, are declared in the Solution class. When declared, these fields are initialized with various values stored in integer literals. But the program doesn't compile, and you need to fix it. To do this, make the fewest possible changes to the va

"OK. I get everything about function calls. But what is this StackTraceElement?"

"The Java Machine keeps track of all function calls. For that, it has a special collection – the stack. When one function calls another, the Java Machine puts a new StackTraceElement object onto the stack. When a function finishes, that element is removed from the stack. This means that the stack always stores up-to-date information about the current state of the 'stack of function calls'."

"Each StackTraceElement object contains information about the called method. In particular, you can get the method name using the getMethodName method."

"You can see how this works in the example above:

1) We get the call stack.

2) We use a for-each loop to go through it. I hope you haven't forgotten what that is.

3) We output the method names to System.out."

"Fascinating! And not too complicated either. Thank you, Rishi!"

undefined
9
Task
New Java Syntax, level 9, lesson 1
Locked
Floating point literals
Seven public fields are declared and initialized in the Solution class. They are initialized with various values stored in floating point literals. But the program doesn't compile, and you need to fix it. To do this, change the field types so they match the values. Do not change the field names or v
undefined
9
Task
New Java Syntax, level 9, lesson 1
Locked
String literals
A public string field is declared and initialized in the Solution class. But the string is too long and difficult to read. For better readability, you need to split it into 5 substrings and concatenate them with the "+" (string concatenation) operator, like this: - first line: "Always write code as
undefined
9
Task
New Java Syntax, level 9, lesson 1
Locked
Character literals
Four public character fields are declared in the Solution class. Some values have been assigned to them. The program doesn't compile. You need to fix this without changing the character values. All the fields are static — this is necessary in order to access them in the main() method. You can see th