最近因为一个小问题,整了个紧急变更,想想还是有点蛋疼,还是记录下警示下自己
代码如下:
Object obj = null;
if (StringUtils.isBlank(String.valueOf(obj))){
System.out.println(1);
}else {
System.out.println(2);
}
具体业务场景就不提了,之前我理所应当以为上面的逻辑会执行1,没想到会走到2,测试也没测出来(涉及到批量操作,我只在catch里面打印了日志),结果就是数据量增到一定程度后,由于打印错误日志造成了接口超时。
回过头看这个问题,我个人一直习惯性的使用String.valueof去转换数据类型,而且我非常相信自己曾经是去看过源码的,但就像是惊鸿一瞥却不在脑袋里留啥印象,造成了这个小杯具。
方法源码如下:
// An highlighted block
/**
* Returns the string representation of the {@code Object} argument.
*
* @param obj an {@code Object}.
* @return if the argument is {@code null}, then a string equal to
* {@code "null"}; otherwise, the value of
* {@code obj.toString()} is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
当传入的对象为null时,方法的返回值是 “null”