
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to Check if an Array is Empty or Not in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to check if an array is empty or not. An array is said to be an empty array, if the array has zero element or no element in it.
Let’s explore the article to see how it can be done by using Java programming language.
To Show You Some Instances
Instance-1
Suppose the original array is {1,3}.
After checking if it is empty or not the result will be −
Array is not empty.
Instance-2
Suppose the original array is {}.
After checking if it is empty or not the result will be −
Array is empty
Instance-3
Suppose the original array is {1,3,4,7,8}.
After checking if it is empty or not the result will be −
Array is not empty
Algorithm
Step 1 − Declare and initialize an integer array.
Step 2 − Get the length of the array.
Step 3 − If length is equal to 0 then array is empty otherwise not.
Step 4 − Print the desired result.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it −
array.length
where, ‘array’ refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Array and length() Method.
By Using User Defined Method and length() Method.
By Using null Check.
Let’s see the program along with its output one by one.
Approach-1: By Using Static Array and length() Method
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm check if an array is empty or not.
public class Main { //main method public static void main(String[] args) { //Declare and initialize the array elements int arr[] = {1,3,4,7,8}; //check if array is empty or not if(arr.length == 0) { //if length is zero then array is empty. System.out.println("Array is empty."); } else { //otherwise array is not empty. System.out.println("Array is not empty."); } } }
Output
Array is not empty.
Approach-2: By Using User Defined Method and length() Method
Example
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm check if an array is empty or not.
public class Main{ //main method public static void main(String[] args) { //Declare and initialize the array elements int arr[] = {}; //callin user defined method func(arr); } //user defined method public static void func(int arr[]){ //check if array is empty or not if(arr.length == 0) { //if length is zero then array is empty. System.out.println("Array is empty."); } else { //otherwise the array is not empty. System.out.println("Array is not empty."); } } }
Output
Array is empty.
Approach-3: By Using null Check
Example
Here array is declared as null means no element. Take an if condition and with the help of equal to operator check array is null or not.
public class Main{ public static void main(String[] args){ int arr[] = null; //check if array is equal to null or not by using equal to operator if(arr == null) { System.out.println("Empty array"); } else { System.out.println("Not an empty array"); } } }
Output
Empty array
In this article, we explored how to check if an array is empty or not in Java.
- Related Articles
- How to check an array is empty or not using jQuery?
- Java Program to check if a string is empty or not
- How to check cursor array list is empty or not in Android sqlite?
- MySQL query to check if database is empty or not?
- How to check if a text field is empty or not in swift?
- How can we check if a JSON object is empty or not in Java?\n
- Check if a directory is not empty in Java
- Check if an array is synchronized or not in C#
- How to check if an URL is valid or not using Java?
- Swift Program to Check if an array is empty
- Check whether a Stack is empty or not in Java
- Check whether a HashSet is empty or not in Java
- Check whether IdentityHashMap empty or not in Java?
- Different ways of checking if an array is empty or not in PHP
- Python program to check if the string is empty or not
