Java (String) 还是 .toString()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(String) or .toString()?
提问by Vugluskr
I have a method with an Object o
parameter.
我有一个带Object o
参数的方法。
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.
在这种方法中,我确切地知道String
在“o”中有一个不为空的。没有必要检查,或做其他事情。我必须完全像对待一个String
对象一样对待它。
Just curious - what is cheaper - cast it to String
, or use Object.toString()
?
Or is it same by time-/cpu-/mem- price?
只是好奇 - 什么更便宜 - 将它投射到String
,还是使用Object.toString()
?还是时间-/cpu-/mem-价格相同?
Update:
The method accepts Object
because it's the implementation of an interface. There is no way to change the parameter type.
更新:该方法接受,Object
因为它是接口的实现。无法更改参数类型。
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.
而且根本不可能null
。我只是想说我不需要检查它是否为空或空。就我而言,总是有一个非空字符串。
采纳答案by euphoria83
casting to a String is cheapersince that doesn't require an external function call, just internal type checking.
转换为 String 更便宜,因为它不需要外部函数调用,只需要内部类型检查。
回答by Ed S.
There cannot be a 'null string in o'. If o is null, it does not contain a null string, it is just null. Just check o for null first. If you castor call ToString() on null you will crash.
o 中不能有“空字符串”。如果 o 为空,则它不包含空字符串,它只是空。只需先检查 o 是否为空。如果你投上null或调用toString(),你会崩溃。
回答by Andy White
If you know the Object o is a String, I'd say just cast it to a String and enforce it that way. Calling toString() on an object that you know for sure is a String might just add confusion.
如果您知道 Object o 是一个字符串,我会说只需将其强制转换为字符串并以这种方式强制执行即可。在您确定是 String 的对象上调用 toString() 可能只会增加混淆。
If Object o might be anything other than a String, you'll need to call toString().
如果对象 o 可能不是字符串,则需要调用 toString()。
回答by Henrik Paul
I wouldn't be too concerned by the performance, if this operation is done even just a few thousand times a second - there's no tangible difference.
我不会太在意性能,如果这个操作每秒只执行几千次 - 没有明显的区别。
I would, however, be concerned of "knowing" the input. You have a method that accepts an Object
and you should treat it as such, i.e. you shouldn't knowanything about the parameter, other than it adheres to the Object
interface, which happens to have a toString()
method. In this case, I would strongly suggest using that method instead of just assuming anything.
但是,我会担心“知道”输入。你有一个接受 an 的方法,Object
你应该这样对待它,即你不应该 知道关于参数的任何事情,除了它坚持Object
接口,它恰好有一个toString()
方法。在这种情况下,我强烈建议使用该方法而不是仅仅假设任何事情。
OTOH, if the input is alwayseither String
or null
, just change the method to accept String
s, and check explicitly for null
s (which you should do anyways whenever dealing with non-primitives...)
OTOH,如果输入始终是 String
or null
,只需将方法更改为接受String
s,并明确检查null
s(无论何时处理非原语,您都应该这样做......)
回答by Jon Skeet
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.
我会使用演员表。这验证了您的“知识”,即它是一个字符串。如果由于某种原因最终出现错误并且有人传入了字符串以外的内容,我认为抛出异常(强制转换会这样做)比继续使用有缺陷的数据执行更好。
回答by TofuBeer
If what you have in "o" is a String then there is not much of a difference (likely the cast is faster, but that is a VM/Library implementation thing).
如果“o”中的内容是字符串,则没有太大区别(可能转换速度更快,但这是 VM/Library 实现)。
If "o" may not be a String but it is supposed to be a String then the cast is what you want (but you should make the method take a String instead of an Object).
如果“o”可能不是字符串但它应该是字符串,那么强制转换就是您想要的(但您应该使该方法采用字符串而不是对象)。
If "o" could be any type then you have to use the toString - but be sure to check for null first.
如果“o”可以是任何类型,那么您必须使用 toString - 但一定要先检查 null。
void foo(final Object o)
{
final String str;
// without this you would get a class cast exception
// be wary of using instanceof though - it is usually the wrong thing to do
if(o instanceof String)
{
str = (String)o;
}
}
or
或者
void foo(final Object o)
{
final String str;
// if you are 100% sure that o is not null then you can get rid of the else
if(o != null)
{
str = o.toString();
}
}
I'd rather code the last one as:
我宁愿将最后一个编码为:
void foo(final Object o)
{
final String str;
if(o == null)
{
throw new IllegalArgumentException("o cannot be null");
}
str = o.toString();
}
回答by Joshua
I found oddly that the cast was slower than the vtable lookup implied by the tostring call.
我奇怪地发现转换比 tostring 调用隐含的 vtable 查找慢。
回答by mP.
Given that the reference type is an Object and all Objects have a toString() just call object.toString(). String.toString() just returns this.
鉴于引用类型是一个对象并且所有对象都有一个 toString() 只需调用 object.toString()。String.toString() 只是返回这个。
- 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.
- toString() 是较少的输入代码。
- toString() 是较少的字节码。
- 铸造是一项昂贵的操作 VS 多态调用。
- 演员阵容可能会失败。
- 使用 String.valueOf( object ) ,如果它不为 null 则只调用 object.toString() 。
回答by cletus
According to Silly performance musings: x.toString() vs (String)x
根据愚蠢的表现思考:x.toString() vs (String)x
In thend end, results are surprisingly clear: it is at least twice as fast to cast Object to String than to call Object.toString()
最后,结果出人意料地清楚:将 Object 转换为 String 的速度至少是调用 Object.toString() 的两倍