toString() 调用:
java.lang.StringBuilder#append() 或 java.lang.StringBuffer#append() 方法中java.io.PrintWriter 或 java.io.PrintStream 的方法中org.slf4j.Logger 的方法中在这些情况下,转换为字符串将由底层库方法处理,不需要显式调用 toString()。
移除冗余的 toString() 调用有时甚至可以提高性能并减少对象分配。
示例:
System.out.println(this.toString())
在应用快速修复后:
System.out.println(this)
请注意,没有 toString() 调用,代码语义可能会有所不同:如果表达式为 null,则将使用 null 字符串,而不是抛出 NullPointerException。
使用 仅在已知限定符为非 null 时报告 选项以避免对可能为 null 的值发出警告。
在这些情况下移除显式 toString() 会改变运行时语义,从抛出 NullPointException 变为当它为 null 时静默接受该值。