(String)还是. toString() ?

我有一个带有 Object o参数的方法。

在这个方法中,我确切地知道在“ o”中有一个不为空的 String。没有必要进行检查或做其他事情。我必须像对待 String物体一样对待它。

只是好奇-什么是便宜-铸造它到 String,或使用 Object.toString()? 还是按照时间-/cpu-/mem-price?

更新: 该方法接受 Object,因为它是接口的实现。

根本不可能是 null。我只是想说,我不需要检查它为空或空。在我的例子中,总是有一个非空字符串。

35660 次浏览

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 cast or call ToString() on null you will crash.

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.

If Object o might be anything other than a String, you'll need to call toString().

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 know anything 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.

OTOH, if the input is always either String or null, just change the method to accept Strings, and check explicitly for nulls (which you should do anyways whenever dealing with non-primitives...)

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.

casting to a String is cheaper since that doesn't require an external function call, just internal type checking.

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).

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).

If "o" could be any type then you have to use the toString - but be sure to check for null first.

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();
}

I found oddly that the cast was slower than the vtable lookup implied by the tostring call.

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.

According to Silly performance musings: 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()