简体   繁体   中英

What is the best way to check string not null and not empty in Java?

我认为!StringUtils.isEmpty(string)string != null && string.length()!=0哪个对性能更好??

I would prefer string != null && !string.isEmpty() . The intent is clear and why rely on an external library for such a simple thing.

There's no relevant performance difference. When the code gets used a lot, the utility method gets inlined and then it's exactly the same. When it doesn't get used, then nobody cares.

Performance is sometimes important, but such trivialities have no impact. Learn Java benchmarking (JMH) before you even start thinking about optimizations.

In case you already use Apache Commons, then use their utility methods. If you don't than don't bring the library in just because of such a triviality. OTOH there are many other useful things in the library (FWIW I started with some trivial methods from Guava and now I use a substantial portion of it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM