Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...
which one will perform better? typecast or toString?
Vicente Raúl Plata Fonseca [XnT] - 07 May 2007 19:44 GMT
> Object o = "Hello, World";
> ....
[quoted text clipped - 4 lines]
>
> which one will perform better? typecast or toString?
If it's just an object, then surely typecast will perform better.
Tom Hawtin - 07 May 2007 19:58 GMT
> Object o = "Hello, World";
> ....
[quoted text clipped - 4 lines]
>
> which one will perform better? typecast or toString?
Who cares? The chances of it making any difference to your overall
algorithm are vanishingly small. Use the one that makes most sense in
your code.
Tom Hawtin
Eric Sosman - 07 May 2007 20:31 GMT
Harish wrote On 05/07/07 14:16,:
> Object o = "Hello, World";
> ....
[quoted text clipped - 4 lines]
>
> which one will perform better? typecast or toString?
String x3 = String.valueOf(o);
... gives you even more totally irrelevant and pointless
performance questions to worry about.
The thing you *ought* to care about is what effects
the different approaches have. Try changing the first
line to `Object o = null;' and observe that the three
conversions to String yield three different results.
Use the one whose results match what you want.

Signature
Eric.Sosman@sun.com
Lulu58e2 - 07 May 2007 20:44 GMT
> Object o = "Hello, World";
> ....
[quoted text clipped - 4 lines]
>
> which one will perform better? typecast or toString?
I wouldn't be surprised if this wasn't optimized out by the compiler.
Still, .toString() is probably safer because someone, at some point
could change o to something other than a String.
C>
Richard Reynolds - 07 May 2007 20:51 GMT
> Object o = "Hello, World";
> ....
[quoted text clipped - 4 lines]
>
> which one will perform better? typecast or toString?
There is a difference in the two, you can only cast if o really is a String
object whereas you can call toString on any Object, not just a String.
Will o always be a String? If so why not just use o rather than create x2?
Can you change the design so that the compiler knows that o is a String?
Torain - 08 May 2007 01:12 GMT
> Object o = "Hello, World";
> ....
[quoted text clipped - 4 lines]
>
> which one will perform better? typecast or toString?
talking about efficiency,no doubt typecast is better
otherwise,the answer depends on what you gonna do with your code
Mike Schilling - 08 May 2007 04:46 GMT
>> Object o = "Hello, World";
>> ....
[quoted text clipped - 6 lines]
>
> talking about efficiency,no doubt typecast is better
When it comes to questions of efficiency, *never* say "no doubt". Always
measure.
Typecasts are actually more complex than they might appear. In this case,
because String is a final class, it's as simple as
if (x2 == null)
result is null
else if (x2.getClass() == String.class)
result is x2;
else
throw ClassCastException()
but if String weren't final or were (God forbid) an interface, this logic
would get more complicated.
Mike Schilling - 08 May 2007 01:31 GMT
> Object o = "Hello, World";
> ....
[quoted text clipped - 4 lines]
>
> which one will perform better? typecast or toString?
If there's any chance of o being null, use the cast.
Otherwise, you should thank God that everything else is working so well you
can spend time on trivia like this.
Daniel Pitts - 09 May 2007 02:28 GMT
> Object o = "Hello, World";
> ....
[quoted text clipped - 4 lines]
>
> which one will perform better? typecast or toString?
String.valueOf(o) performs better than either of those, because it
should never throw an exception!