Difference between isEmpty() and isBlank() Method in java 11

Java 11 has added A new instance method isBlank() to java.lang.String class.

What's the basic difference between the existing isEmpty and newly added isBlank() method?

1 answer

  • answered 2018-07-12 06:58 Niraj Sonawane

    isEmpty()

    The java string isEmpty() method checks if this string is empty. It returns true, if the length of the string is 0 otherwise false

    e.g

    String str ="";
    
    String str2 ="  ";
    
    System.out.println(str.isEmpty()); // Prints - True
    System.out.println(str2.isEmpty()); //Prints - False 
    

    java 11 - isBlank()

    The new instance method java.lang.String.isBlank()

    returns true if the string is empty or contains only white space, where whitespace is defined as any codepoint that returns true when passed to Character#isWhitespace(int).

    boolean blank = string.isBlank();
    

    Before Java 11

    boolean blank = string.trim().isEmpty();
    

    After Java 11

    boolean blank = string.isBlank();
    

    Reference :isBlank()