String.valueOf()的一个坑—“null”
2016-03-31 14:37
489 查看
public class Entity {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
public static String valueOf(Object obj)
返回 Object 参数的字符串表示形式。
参数:
obj - 一个 Object。
返回:
如果参数为 null,则字符串等于 "null";否则,返回 obj.toString() 的值
[align=left]
[/align]
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
public class Demo { public static void main(String[] args) { Entity entity = new Entity(); System.out.println(String.valueOf(entity.getId())); } }运行的结果为“null”,为字符串类型的null。从代码上咋一看,原本以为会报nullPointer异常。查看JDK文档得知:
public static String valueOf(Object obj)
返回 Object 参数的字符串表示形式。
参数:
obj - 一个 Object。
返回:
如果参数为 null,则字符串等于 "null";否则,返回 obj.toString() 的值
[align=left]
[/align]
相关文章推荐
- 对Map类分别按照Key,Value排序,返回值为List对象
- iOS之NSPredicate(正则表达式和UIBarController)
- Vue.js 添加组件
- ios CAShapeLayer和UIBezierPath
- iOS在更改用户头像并保存至本地沙盒目录中对于UIImagePickerController、UIAlertController的使用
- [UITableView _endCellAnimationsWithContext:]
- iOS基本UI控件总结
- #学习笔记#(56)angular ui-router使用姿势
- An AnnotationConfiguration instance is required to use.....异常
- php中的continue用法
- iOS-一个对UIAlertController的封装类分享
- iOS: setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.
- iOS UITableViewController出现crash
- 从response.header中提取cookie,在request里添加cookie
- 从response.header中提取cookie,在request里添加cookie
- UItableView的两个重用机制区别
- UI练习
- requests用法
- JAVA GUI之CardLayout
- 客户端窗口UI管理