Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is StringUtils.EMPTY recommended?

Do you use StringUtils.EMPTY instead of ""?

I mean either as a return value or if you set a the value of a String variable. I don't mean for comparison, because there we use StringUtils.isEmpty()

like image 690
keuleJ Avatar asked Nov 04 '10 09:11

keuleJ


People also ask

What is StringUtils empty?

StringUtils. EMPTY is a final static member of StringUtil class that equates to "" i.e empty String. whereas. StringUtils. isEmpty() Checks if a String is empty ("") or null.

Does StringUtils is empty check for NULL?

We can check whether a particular String is empty or not, using isBlank() method of the StringUtils class. This method accepts an integer as a parameter and returns true if the given string is empty, or false if it is not.

What is the difference between StringUtils isBlank and StringUtils isEmpty?

StringUtils isEmpty = String isEmpty checks + checks for null. StringUtils isBlank = StringUtils isEmpty checks + checks if the text contains only whitespace character(s).

What is the use of StringUtils?

Most common use of StringUtils is in web projects (when you want to check for blank or null strings, checking if a string is number, splitting strings with some token). StringUtils helps to get rid of those nasty NumberFormat and Null exceptions. But StringUtils can be used anywhere String is used.


1 Answers

Of course not. Do you really think "" is not clear enough ?

Constants have essentially 3 use cases:

  1. Document the meaning of a value (with constant name + javadoc)
  2. Synchronize clients on a common value.
  3. Provide a shortcut to a special value to avoid some init costs

None apply here.

like image 175
David Pierre Avatar answered Nov 04 '22 16:11

David Pierre