(String) or .toString()?

后端 未结 9 1547
慢半拍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:41

    I would use a cast. That validates your "knowledge" that it's a string. If for whatever reason you end up with a bug and someone passes in something other than a string, I think it would be better to throw an exception (which a cast will do) than continue to execute with flawed data.

提交回复
热议问题