
- 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
Java Program to Check if a String is Empty or Null
In this article, we will understand how to check if a string is empty or null. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).
Below is a demonstration of the same −
Suppose our input is −
Input string: null
The desired output would be −
The string is a null string
Algorithm
Step 1 - START Step 2 - Declare a string namely input_string. Step 3 - Define the values. Step 4 - Using an if-loop, compute input_string == null. If true, the string is null, else the string is not null. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class Demo { public static void main(String[] args) { String input_string = null; System.out.println("The string is defined as: " +input_string); if (input_string == null) { System.out.println("\nThe string is a null string"); } else if(input_string.isEmpty()){ System.out.println("\nThe string is an empty string"); } else { System.out.println("\nThe string is neither empty nor null string"); } } }
Output
The string is defined as: null The string is a null string
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class Demo { static void isNullEmpty(String input_string) { if (input_string == null) { System.out.println("\nThe string is a null string"); } else if(input_string.isEmpty()){ System.out.println("\nThe string is an empty string"); } else { System.out.println("\nThe string is neither empty nor null string"); } } public static void main(String[] args) { String input_string = null; System.out.println("The string is defined as: " +input_string); isNullEmpty(input_string); } }
Output
The string is defined as: null The string is a null string
- Related Articles
- Golang program to check if a string is empty or null
- Check if a String is empty ("") or null in Java
- Check if a String is whitespace, empty ("") or null in Java
- Java Program to check if a string is empty or not
- Check if a String is not empty ("") and not null in Java
- Java program to find whether the given string is empty or null
- Python program to check if the string is empty or not
- How to check if field is null or empty in MySQL?
- How to test String is null or empty?
- How to check if String is empty in Java?
- How do I check if a column is empty or null in MySQL?
- Check whether a field is empty or null in MySQL?
- Golang Program to check the given string is empty or not
- Checking for Null or Empty or White Space Only String in Java.
- C# Program to check a string for whitespace characters or null

Advertisements