BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String isEmpty() method

Last Updated: June 9, 2024 by Chaitanya Singh | Filed Under: java

Java String isEmpty() method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false. In other words you can say that this method returns true if the length of the string is 0.

Basic Syntax

boolean isEmpty = str.isEmpty();

Java String isEmpty() method Examples

1. Checking if a String is Empty

public class Example{  
public static void main(String args[]){
//empty string
String str1="";
//non-empty string
String str2="hello";

// Output: true
System.out.println(str1.isEmpty());
// Output: false
System.out.println(str2.isEmpty());
}
}

2. Checking if a string is null or empty

In the above example, we used isEmpty() method to check if String is empty or not. However If you want to check whether the String is null or empty both then you can do it as shown in the following example. I have also covered this in this article: How to check if string is null, empty or blank.

public class Example{  
   public static void main(String args[]){  
	String str1 = null; 
	String str2 = "beginnersbook";  

	if(str1 == null || str1.isEmpty()){
	   System.out.println("String str1 is empty or null"); 
	}
	else{
	   System.out.println(str1);
	}
	if(str2 == null || str2.isEmpty()){
	   System.out.println("String str2 is empty or null");  
	}
	else{
	   System.out.println(str2);
	}
   }
}

Output:

String str1 is empty or null
beginnersbook

3. Checking for Only Whitespace

Sometimes a string may contain whitespaces, in such cases you may want to check if a string is empty or contains only whitespace. For this purpose, you can use trim()along with isEmpty().

public class Example{  
public static void main(String args[]){
String str1 = " ";
String str2 = "text";

boolean isEmptyOrWhitespace1 = (str1.trim().isEmpty()); // true
boolean isEmptyOrWhitespace2 = (str2.trim().isEmpty()); // false

// Output: str1 is empty or whitespace: true
System.out.println("str1 is empty or whitespace: " + isEmptyOrWhitespace1);

// Output: str2 is empty or whitespace: false
System.out.println("str2 is empty or whitespace: " + isEmptyOrWhitespace2);
}
}

Summary

The String.isEmpty() method is a simple method to check if a string is empty in Java. It is generally used along with trim() method and null checks to validate string content.

❮ Java String Class

Top Related Articles:

  1. String Concatenation in Java
  2. Java Math.sin() Method
  3. Java String charAt() Method example
  4. Java String indexOf() Method
  5. Java 8 StringJoiner with example

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap