GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Stack empty() Method in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Stack empty() Method in Java

Last Updated : 22 Jan, 2026

The java.util.Stack.empty() method is used to check whether a stack contains any elements. It returns a boolean value—true if the stack is empty, and false otherwise. This method is commonly used to avoid EmptyStackException, control stack traversal, and implement safe error handling.

Syntax:

stack.empty()

  • Parameters: The method does not take any parameters. 
  • Return Value: The method returns true if the stack is empty, else it returns false. 

Example 1: Checking Stack Emptiness Before and After Pop

Java
import java.util.Stack;

public class StackDemo {
    public static void main(String[] args) {

        Stack<String> stack = new Stack<>();

        stack.push("Geeks");
        stack.push("4");
        stack.push("Geeks");
        stack.push("Welcomes");
        stack.push("You");

        System.out.println("Stack: " + stack);
        System.out.println("Is stack empty? " + stack.empty());

        while (!stack.empty()) {
            stack.pop();
        }

        System.out.println("Is stack empty? " + stack.empty());
    }
}

Output
Stack: [Geeks, 4, Geeks, Welcomes, You]
Is stack empty? false
Is stack empty? true

Example 2: Checking Emptiness of an Integer Stack

Java
import java.util.Stack;

public class StackDemo {
    public static void main(String[] args) {

        Stack<Integer> stack = new Stack<>();

        stack.push(8);
        stack.push(5);
        stack.push(9);
        stack.push(2);
        stack.push(4);

        System.out.println("Stack: " + stack);
        System.out.println("Is stack empty? " + stack.empty());
    }
}

Output
Stack: [8, 5, 9, 2, 4]
Is stack empty? false

Example 3: Traversing a Stack Using empty() (Sum of Elements)

Java
import java.util.Stack;

public class StackDemo {
    public static void main(String[] args) {

        Stack<Integer> stack = new Stack<>();

        stack.push(23);
        stack.push(3);
        stack.push(-30);
        stack.push(13);
        stack.push(45);

        System.out.println("Stack: " + stack);

        int sum = 0;

        while (!stack.empty()) {
            sum += stack.pop();
        }
        System.out.println("Sum of elements: " + sum);
        System.out.println("Is stack empty? " + stack.empty());
    }
}

Output
Stack: [23, 3, -30, 13, 45]
Sum of elements: 54
Is stack empty? true

Example 4: Using empty() for Error Handling

The empty() method is useful for validating input scenarios, such as checking whether a file contains data before processing.

Java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;

public class FileReadExample {

    public static void main(String[] args) {

        Stack<String> stack = new Stack<>();

        try {
            File file = new File("data.txt");
            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                stack.push(scanner.nextLine());
            }
            scanner.close();

        } catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }

        if (stack.empty()) {
            System.out.println("File is empty.");
        } else {
            System.out.println("File is not empty. Stack contents:");
            while (!stack.empty()) {
                System.out.println(stack.pop());
            }
        }
    }
}

Output
File not found.
File is empty.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.