Explicit vs implicit call of toString

前端 未结 5 871
日久生厌
日久生厌 2020-12-01 04:31

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 i

5条回答
  •  隐瞒了意图╮
    2020-12-01 04:43

    As others have said - use the "" + obj method.

    According to The Java Language Spec:

    • If the term is null, use "null"
    • Primitive types are converted using the boxed-type constructor new Boolean(X) or whatever
    • toString() is invoked (or equivalent)
    • if the result of toString() is null, use "null"
    • Concatenate the strings.

提交回复
热议问题