🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
In this guide, you will learn about the String isEmpty() method in Java programming and how to use it with an example.
1. String isEmpty() Method Overview
Definition:
The isEmpty() method in Java's String class is utilized to check if a given string has a length of zero, essentially determining whether the string is empty or not.
Syntax:
str.isEmpty()
Parameters:
- The method takes no parameters.
Key Points:
- Returns true if the string has a length of 0, otherwise false.
- It's a more readable alternative to checking the string length using str.length() == 0.
- Useful in scenarios like form validation, parsing input, etc., to ensure data presence.
2. String isEmpty() Method Example
public class IsEmptyExample {
public static void main(String[] args) {
String emptyString = "";
String nonEmptyString = "Java";
String whitespaceString = " ";
// Checking an empty string
boolean isEmpty1 = emptyString.isEmpty();
System.out.println("Is the emptyString empty? " + isEmpty1);
// Checking a non-empty string
boolean isEmpty2 = nonEmptyString.isEmpty();
System.out.println("Is the nonEmptyString empty? " + isEmpty2);
// Checking a whitespace string
boolean isEmpty3 = whitespaceString.isEmpty();
System.out.println("Is the whitespaceString empty? " + isEmpty3);
}
}
Output:
Is the emptyString empty? true Is the nonEmptyString empty? false Is the whitespaceString empty? false
Explanation:
In the example:
1. We first check an empty string using the isEmpty() method. As expected, the method returns true for the empty string.
2. Next, we check a non-empty string "Java". Since the string contains characters, the method returns false.
3. Lastly, we evaluate a string containing just a whitespace. It's essential to note that the isEmpty() method considers a string with whitespace as non-empty. Therefore, it returns false.
Related Java String Class method examples
- Java String charAt() example
- Java String concat() example
- Java String contains() example
- Java String endsWith() example
- Java String equals() example
- Java String equalsIgnoreCase() example
- Java String getBytes() example
- Java String indexOf() example
- Java String isEmpty() example
- Java String lastIndexOf() example
- Java String length() example
- Java String replace() example
- Java String split() example
- Java String startsWith() example
- Java String substring() example
- Java String toLowerCase() example
- Java String toUpperCase() example
- Java String trim() example
- Java String valueOf() example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment