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

后端 未结 3 1861
悲&欢浪女
悲&欢浪女 2020-12-10 01:50

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

What\'s the basic difference between the exi

3条回答
  •  执念已碎
    2020-12-10 02:45

    Java 11 added has new method called .isBlank() in String class

    1. isBlank() method is equal to str.trim().isEmpty() in earlier to java 11 versions
    2. isEmpty() : Returns true if, and only if, length() is 0

    This is the internal implementation of isBlank() method in String class of java 11

    public boolean isBlank() {
        return indexOfNonWhitespace() == length();
    }
    
    private int indexOfNonWhitespace() {
        if (isLatin1()) {
            return StringLatin1.indexOfNonWhitespace(value);
        } else {
            return StringUTF16.indexOfNonWhitespace(value);
        }
    }
    

提交回复
热议问题