Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 532
Niraj Sonawane Avatar asked Jul 12 '18 06:07

Niraj Sonawane


People also ask

Which is better isEmpty or isBlank?

Practically speaking, you should generally use String. isBlank if you expect potentially whitespace strings (e.g. from user input that may be all whitespace), and String. isEmpty when checking fields from the database, which will never contain a string of just whitespace.

What is isBlank () in Java?

isBlank() is an instance method that returns true if the string is empty or contains only white space codepoints. This method was introduced in Java 11. If the string contains only white spaces, then applying this method will return true .

What is difference between StringUtils isEmpty and isBlank?

StringUtils isEmpty() Example in Java.The isEmpty() method doesn't check for whitespace. It will return false if our string is whitespace. We need to use StringUtils isBlank() method to check whitespace.

Which of the following statement is related to the isBlank () method in Java 11?

In Java 11, a new method called isBlank() was added in the String class. The isBlank() method will return true in the below cases: If the string is empty. If the string only contains whitespaces.


1 Answers

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.

System.out.println("".isEmpty()); // Prints - True System.out.println(" ".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(); 
like image 52
Niraj Sonawane Avatar answered Sep 21 '22 19:09

Niraj Sonawane