Java String isempty() Method with Example – Syntax, Definition, Usage

String isempty java: In the last tutorial, we have learned how to compare two strings in Java. In this tutorial, we will learn how to check if the string is empty or not using the String isempty(). Learn about the syntax, parameters of the String isempty() and how it works internally and the implementation code used, etc.

String isempty() Definition & Usage

Java string isempty: The isEmpty() method of the String class is used to check whether the string is empty or not. If the string is empty then the isEmpty() method returns true otherwise it returns false. In other words, we can say that this method returns true if the string length is 0.

Syntax: The syntax of the isEmpty() of string class is given below

public boolean isEmpty()

How isEmpty Works Internally and Implementation Code?

Java string.isempty: isEmpty internal code is much straightforward. Check the below code to know about its internal implementation and how it works internally.

public boolean isEmpty() {
return value.length == 0;
}

Check the String Length and If the String Length is 0 return true or else return false. This method will not check for blank spaces.

Do Read:

Example of String isEmpty() Method in Java

class StringEmpty{
public static void main(String args[]){
// Non-empty string.
String str1 = "Javastudypoint";
//empty string.
String str2 = "";
//returns false.
System.out.println(str1.isEmpty());
//retuens true.
System.out.println(str2.isEmpty());
}
}

Output:

Java String isempty() method with Example

String isEmpty Method Example Program to check if the String is Null or Not

package examples.java.w3schools.string;

public class StringisBlankExample {
public static void main(String[] args) {
// String 1 - null string - no value assigned
String nullString = null;

// String 2
String valueString = "value string";

// printing values
System.out.println("nullString :: " + nullString);
System.out.println("valueString :: " + valueString);

// Use case : Using if condition.

if (nullString == null || nullString.isEmpty()) {
System.out.println("nullString is null or blank");
} else {
System.out.println("nullString is having some value");
}

if (valueString == null || valueString.isEmpty()) {
System.out.println("valueString is null or blank");
} else {
System.out.println("valueString is having some value");
}

}
}

Output:

nullString :: null
valueString :: value string
nullString is null or blank
valueString is having some value