This question has bogged me quite a while. During programming there is regularly the question whether there is something in an object or not. For this reason was the isEmpty
method invented. Great, but in practice we use it like !isEmpty
almost all the time.
As a consequence, notEmpty
would be a much more appreciated addition to a language like Java. The question is: why don't language designers think of this before defining the API? At least give a counterpart for isEmpty
EDIT: I meant there should be a notEmpty
as well as isEmpty
. Depending on the domain, both of them may be used but in most cases when a UI is not involved, I think notEmpty
applies better.
EDIT2: To close the discussion, here is an example:
!metadata.isEmpty() == metadata.notEmpty()
I'd prefer we had the right side of the equation as well.
isBlank() vs isEmpty() Both methods are used to check for blank or empty strings in java. The difference between both methods is that isEmpty() method returns true if, and only if, string length is 0. isBlank() method only checks for non-whitespace characters.
The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.
The difference between null and empty is that the null is used to refer to nothing while empty is used to refer a unique string with zero length.
isEmpty(<string>) Checks if the <string> value is an empty string containing no characters or whitespace. Returns true if the string is null or empty.
isEmpty
follows the naming convention of other syntactically positive isXXX
methods such as isVisible and isEnabled
I think it follows the convention of using is[Something]()
for booleans in place of get[Something]()
.
Also, having to use !notEmpty()
might get a bit confusing!
My opinion is, it is more logical because like ==
and !=
. Logically It is better to have isEmpty
and !isEmpty
, It is easy to understand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With