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

Code Example 1

import java.util.Scanner;
import java.util.ArrayList;
public class BalanceBracket {
public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	System.out.println("enter a string with '(', '{' or '['");
	String s = sc.nextLine();
	String y = isBalanced(s);
	System.out.println(y);
}
static String isBalanced(String s)
{
	String result = "NO";
	ArrayList<String> al = new ArrayList<String>();
	
	for(int i = 0; i < s.length(); i++)
	{
		if(s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[')
        {
			al.add(Character.toString(s.charAt(i)));
		}
		
		else if(s.charAt(i) == ')' || s.charAt(i) == '}' || s.charAt(i) == ']')
		{
			if(al.size() != 0 && al.get(al.size()-1).equals("(") && s.charAt(i) == ')')
			{
				al.remove(al.size()-1);
			}
			else if(al.size() != 0 && al.get(al.size()-1).equals("{") && s.charAt(i) == '}')
			{
				al.remove(al.size()-1);
			}
			else if(al.size() != 0 && al.get(al.size()-1).equals("[") && s.charAt(i) == ']')
			{
				al.remove(al.size()-1);
			}
			else
			{
				return result = "NO";
			}
		}
		
	}
	if(al.isEmpty() == true)
	{
		return result = "YES";
	}
	return result;
}
}

Code Example 2

// Java program for checking 
// balanced Parenthesis 
import java.util.*; 
  
public class BalancedParan { 
  
    // function to check if paranthesis are balanced 
    static boolean areParanthesisBalanced(String expr) 
    { 
        // Using ArrayDeque is faster than using Stack class 
        Deque<Character> stack = new ArrayDeque<Character>(); 
  
        // Traversing the Expression 
        for (int i = 0; i < expr.length(); i++) { 
            char x = expr.charAt(i); 
  
            if (x == '(' || x == '[' || x == '{') { 
                // Push the element in the stack 
                stack.push(x); 
                continue; 
            } 
  
            // IF current current character is not opening 
            // bracket, then it must be closing. So stack 
            // cannot be empty at this point. 
            if (stack.isEmpty()) 
                return false; 
  
            switch (x) { 
            case ')': 
                stack.pop(); 
                if (x == '{' || x == '[') 
                    return false; 
                break; 
  
            case '}': 
                stack.pop(); 
                if (x == '(' || x == '[') 
                    return false; 
                break; 
  
            case ']': 
                stack.pop(); 
                if (x == '(' || x == '{') 
                    return false; 
                break; 
            } 
        } 
  
        // Check Empty Stack 
        return (stack.isEmpty()); 
    } 
  
    /*driver program to test above functions*/
    public static void main(String[] args) 
    { 
        String expr = "([{}])"; 
        if (areParanthesisBalanced(expr)) 
            System.out.println("Balanced "); 
        else
            System.out.println("Not Balanced "); 
    } 
}

Learn ReactJs, React Native from akashmittal.com