null object also has a toString() function?!
来源:互联网 发布:免费图片库 知乎 编辑:程序博客网 时间:2024/06/06 00:55
import static java.lang.System.out;
public class TestNull {
public static void main(String[] args) {
String s = null;
String t = "";
out.println(s);
out.println(t);
out.println(s + 1);
out.println(t + 1);
}
}
the output is:
null
null1
1
I used to use the implicit call of toString when wanting some debug info about an object, because in case of the object is null it does not throw an Exception.
For instance:
System.out.println("obj: "+obj);
instead of:
System.out.println("obj: "+obj.toString());
Is there any difference apart from the null case?
Can the latter case work, when the former does not?
Edit:
What exactly is done, in case of the implicit call?
========================================
There's little difference. Use the one that's shorter and works more often.
If you actually want to get the string value of an object for other reasons, and want it to be null friendly, do this:
String s = String.valueOf(obj);
The question was extended, so I'll extend my answer.
In both cases, they compile to something like the following:
System.out.println(new StringBuilder().append("obj: ").append(obj).toString());
When your toString()
is implicit, you'll see that in the second append.
If you look at the source code to java, you'll see that StringBuilder.append(Object)
looks like this:
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
where String.valueOf
looks like this:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Now, if you toString()
yourself, you bypass a null check and a stack frame and go straight to this inStringBuilder
:
public StringBuilder append(String str) {
super.append(str);
return this;
}
So...very similar things happens in both cases. One just does a little more work.
- null object also has a toString() function?!
- The given object has a null identifier
- The given object has a null identifier:
- The given object has a null identifier
- The given object has a null identifier
- The given object has a null identifier:
- The given object has a null identifier解决之法
- The given object has a null identifier解决办法
- org.hibernate.TransientObjectException: The given object has a null identifier:
- java中的异常The given object has a null identifier
- org.hibernate.TransientObjectException:The given object has a null identifier
- org.hibernate.TransientObjectException: The given object has a null identifier:
- org.hibernate.TransientObjectException: The given object has a null identifier;
- org.hibernate.TransientObjectException: The given object has a null identifier:
- org.hibernate.TransientObjectException:The given object has a null identifier
- The given object has a null identifier问题解决
- 异常:The given object has a null identifer...
- The given object has a null identifier解决之法
- Android2.2平台上支持多camera(一)
- 智慧地球战略的简介
- Android2.2平台上支持多camera(二)
- c语言基础 2
- Android2.2平台上支持多camera(三)
- null object also has a toString() function?!
- Android2.2平台上支持多camera(四)
- 我的软件设计心得:我们所做的,就是客户所要的!
- Sybase常用sql语句
- DATEDIFF() 函数用法
- 单链表(不含头结点)(1)
- java算法测试题
- 摘要 from MAC address wiki
- 再看Effective C++(1)