String method isEmpty() in Java
The String method isEmpty() used to determine whether string is empty or with zero legnth. It returns boolean value, if string is empty or zero length. It returns NullPointerException if string is not initialized.
Syntax
 1 public boolean isEmpty()
return : It returns a boolean true, if string length is zero, otherwise false.
Example
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6 
 7         String str = ""; // zero length string
 8         String str1 = " "; // string with white space
 9         String str2 = "Hello world";
 10 
 11         boolean res1 = str.isEmpty();
 12         System.out.println("A zero length string is empty : " + res1);
 13 
 14         boolean res2 = str1.isEmpty();
 15         System.out.println("Only white space string is empty : " + res2);
 16 
 17         boolean res3 = str2.isEmpty();
 18         System.out.println("Unicode character string is empty : " + res3);
 19     }
 20 }
In the above example, a string objects define with zero length, string with white space and string with unicode characters. A string instance method isEmpty() called that determine whether a string is zero length string. If a string is zero length, it returns true, otherwise false.
Output
 1 A zero length string is empty : true
 2 Only white space string is empty : false
 3 Unicode character string is empty : false

isBlank() method

isBlank() method
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6 
 7         String str = ""; // zero length string
 8         String str1 = " "; // string with white space
 9         String str2 = "Hello world";
 10 
 11         boolean res1 = str.isBlank();
 12         System.out.println("A zero length string is blank : " + res1);
 13 
 14         boolean res2 = str1.isBlank();
 15         System.out.println("Only white space string is blank : " + res2);
 16 
 17         boolean res3 = str2.isBlank();
 18         System.out.println("Unicode character string is blank : " + res3);
 19     }
 20 }
In the above example, a string class isBlank() method called that determine whether string is zero length or string includes only white spaces. It returns boolean value true, otherwise false.
Output
 1 A zero length string is blank : true
 2 Only white space string is blank : true
 3 Unicode character string is blank : false

String method isEmpty() with collection

String method isEmpty() with collection
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class Example {
 7 
 8     public static void main(String[] args) {
 9 
 10         List<String> list = Arrays.asList("This is", "", "String");
 11 
 12         list.forEach(str -> {
 13             System.out.println("Does string empty : " + str.isEmpty());
 14         });
 15     }
 16 }
In the above example, a list of string literals define and iterated using collection method forEach() by specifying a lambda expression. It validates whether a string length is zero and returns boolean value that will be print.
Output
 1 Does string empty : false
 2 Does string empty : true
 3 Does string empty : false

String method isEmpty() with Stream API

String method isEmpty() with Stream API
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class StreamExp {
 7 
 8     public static void main(String[] args) {
 9 
 10         List<String> list = Arrays.asList("This is", "", "string");
 11 
 12         list.stream().map(str -> "Does string empty : " + str.isEmpty())
 13                 .forEach(System.out::println);
 14     }
 15 }
In the above example, a List of string literals define and collection method stream() called that returns Stream object. A Stream method map() called by specifying lambda expression that validate whether a string is empty and returns a resulting string with boolean value. A resulting Stream object iterated using forEach() method and print each elements.
Output
 1 Does string empty : false
 2 Does string empty : true
 3 Does string empty : false
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us