Check out example codes for "stack class in java". It will help you in understanding the concepts better.

Code Example 1

import java.util.Stack<E>;
Stack<Integer> myStack = new Stack<Integer>();
myStack.empty(); // True if stack is empty

Code Example 2

import java.util.Stack;
class Main {
    public static void main(String[] args) {
        Stack<String> animals= new Stack<>();
        // Add elements to Stack
        // Remove element from Stack
      	// Access element from top of Stack

Code Example 3

// construct with non-primative elements only!
Stack<String> stack = new Stack<String>();

// to add a value to the top of the stack:

// to return and remove a value from the top:
String top = stack.pop();

// to return a value without removing it:
String peek = stack.peek();

Code Example 4

// Java code to illustrate push() method 
import java.util.*; 
public class StackDemo { 
    public static void main(String args[]) 
        // Creating an empty Stack 
        Stack<Integer> STACK = new Stack<Integer>(); 
        // Use push() to add elements into the Stack 
        // Displaying the Stack 
        System.out.println("Initial Stack: " + STACK); 
        // Pushing elements into the Stack 
        // Displaying the final Stack 
        System.out.println("Final Stack: " + STACK); 

Learn ReactJs, React Native from