(String)或.toString()? [英] (String) or .toString()?
问题描述
我有一个有 Object o
参数的方法。
I have a method with an Object o
parameter.
在这个方法中,在不为空的o中的 String
。没有必要检查,或做别的事情。我必须像一个 String
对象一样对待它。
In this method, I exactly know there is a String
in "o" which is not null. There is no need to check, or do something else. I have to treat it exactly like a String
object.
只是好奇 - code> String ,或使用 Object.toString()
?
或者是同样的time- / cpu- / mem-price?
Just curious - what is cheaper - cast it to String
, or use Object.toString()
?
Or is it same by time-/cpu-/mem- price?
更新:
该方法接受 Object
,因为它是一个接口的实现。没有办法更改参数类型。
Update:
The method accepts Object
because it's the implementation of an interface. There is no way to change the parameter type.
它不能是 null
。我只是想说,我不需要检查它为null或空。在我的例子中,总是有一个非空字符串。
And it can't be null
at all. I just wanted to say that I do not need to check it for null or emptyness. In my case, there is always a nonempty string.
推荐答案
不需要外部函数调用,只需进行内部类型检查。
casting to a String is cheaper since that doesn't require an external function call, just internal type checking.
这篇关于(String)或.toString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!