Home » Java programming language

Java Stack empty() Method with Example

Stack Class empty() method: Here, we are going to learn about the empty() method of Stack Class with its syntax and example.
Submitted by Preeti Jain, on March 24, 2020

Stack Class empty() method

  • empty() method is available in java.util package.
  • empty() method is used to check whether this stack is empty or not empty.
  • empty() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • empty() method does not throw an exception at the time of checking empty status.

Syntax:

    public boolean empty();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is boolean, it returns true when this Stack is empty otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean empty() method of Stack Class

import java.util.*;

public class IsEmptyOfStack {
 public static void main(String args[]) {
  // Instantiates Stack object
  Stack s = new Stack();

  // By using push() method isto
  // push the given onto the stack
  s.push(10);
  s.push(20);
  s.push(30);
  s.push(40);
  s.push(50);

  // Display Stack
  System.out.println("s:" + s);

  // By using isEmpty() method isto
  // check the empty status of the
  // Stack
  boolean status = s.isEmpty();

  // Display Status
  System.out.println("s.isEmpty(): " + status);
 }
}

Output

s:[10, 20, 30, 40, 50]
s.isEmpty(): false


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.