Stack is a class in Java that works based on the LIFO (Last In, First Out) principle. This means the element that is added last will be the first one to be removed.
Values can be added to a stack using the push() method.
Example code:
Stack<Integer> stack = new Stack<>();
stack.push(3); // adding the values
stack.push(1);
stack.push(2);
System.out.print(stack); // prints [3, 1, 2]
sukanya meruva Answered question