Make String.format(“%s”, arg) display null-valued arguments differently from “null”

前端 未结 10 1198
忘了有多久
忘了有多久 2021-02-06 21:22

Consider the custom toString() implementation of a bean:

@Override
public String toString() {
    String.format(\"this is %s\", this.someField);
}
<         


        
10条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-06 22:00

    From java 7, you can use Objects.toString(Object o, String nullDefault).

    Applied to your example: String.format("this is %s", Objects.toString(this.someField, "?"));

提交回复
热议问题