為什么不建議你用去 “! = null” 做判空?
閱讀本文大概需要 3.5 分鐘。
來自:stackoverflow
...if (someobject != null) {
someobject.doCalc();}...public interface Action {
void doSomething();}
public interface Parser {
Action findAction(String userInput);}public class MyParser implements Parser {
private static Action DO_NOTHING = new Action() {
public void doSomething() { /* do nothing */ }
};
public Action findAction(String userInput) {
// ...
if ( /* we can't find any actions */ ) {
return DO_NOTHING;
}
}}Parser parser = ParserFactory.getParser();
if (parser == null) {
// now what?
// this would be an example of where null isn't (or shouldn't be) a valid response
}
Action action = parser.findAction(someInput);
if (action == null) {
// do nothing} else {
action.doSomething();}ParserFactory.getParser().findAction(someInput).doSomething();
"bar".equals(foo)
foo.equals("bar")推薦閱讀:
內容包含Java基礎、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊列、高性能緩存、反射、Spring全家桶原理、微服務、Zookeeper、數(shù)據(jù)結構、限流熔斷降級......等技術棧!
?戳閱讀原文領??!? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??朕已閱?
評論
圖片
表情

