(String) or .toString()?

后端 未结 9 1542
慢半拍i
慢半拍i 2020-12-12 16:56

I have a method with an Object o parameter.

In this method, I exactly know there is a String in \"o\" which is not null. There is no need t

9条回答
  •  暖寄归人
    2020-12-12 17:32

    Given that the reference type is an Object and all Objects have a toString() just call object.toString(). String.toString() just returns this.

    • toString() is less code to type.
    • toString() is less bytecode.
    • casting is an expensive operation VS a polymorphic call.
    • the cast could fail.
    • Use String.valueOf( object ) which just calls object.toString() if its not null.

提交回复
热议问题