
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a String Is Empty or Not in Java
In this article, we will learn how to check if a String is empty in Java. The isEmpty() method is used to check whether a given string does not contain characters.
Problem Statement
Given a string, write a Java program to determine if the string is empty.Input
String str = ""Output
String is empty: true
Steps to check if a string is empty
The following are the steps to check if a string is empty ?- Create a string.
- Use the isEmpty() method to check whether the string has no characters.
- Print the result, which will be either true if the string is empty or false if it has characters.
Java program to check if a string is empty
The following is an example of checking if a string is empty
public class Demo { public static void main(String[] args) { String str = ""; System.out.println("String is empty: "+str.isEmpty()); } }
Output
String is empty: true
Code Explanation
String str = ""; is used in this example to define a string that contains no characters. The isEmpty() method checks if the string's length is zero. If the string is empty, the method returns true. We then print the result. If the string had contained any characters, the method would have returned false instead of true.Advertisements