Java Vector isEmpty() Method Tutorial

In this section, we will learn what the Vector isEmpty() method is and how to use it in Java.

What is Java Vector isEmpty() Method?

The Java Vector isEmpty() method is used to check a vector object and see if it is empty of any value or not.

Note: calling the setSize() on a vector object to set a new size for it might add null element if the new size is bigger than the old size. In a case like this, even if the entire elements of the vector object is null, this method considers the object as not empty!

Java Vector isEmpty() Method Syntax:

public boolean isEmpty();

isEmpty() Method Parameters:

The method does not take an argument.

isEmpty() Method Return Value:

The return value of this method is of Boolean type.

If the target vector object is empty of values (not even a single null value) then the return value of this method is true. Otherwise the value false will return.

isEmpty() Method Exceptions:

The method does not throw an exception.

Example: using Vector isEmpty() method

import java.util.Vector; 
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.setSize(30);
        System.out.println(vector.isEmpty());
    }
}

Output:

false

In this example, we didn’t add any value to the vector object but just changed its size to 30.

Now behind the scene at the execution time, Java will set null values for each of these 30 elements.

And for that reason, this vector object is not considered as an empty object anymore.

Leave a Reply