toString or typecast: which one is better

H

Harish

Object o = "Hello, World";
.....
String x1 = o.toString();
// or
String x2 = (String)o;
....

which one will perform better? typecast or toString?
 
?

=?iso-8859-1?q?Vicente_Ra=FAl_Plata_Fonseca_[XnT]?

Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...

which one will perform better? typecast or toString?

If it's just an object, then surely typecast will perform better.
 
T

Tom Hawtin

Harish said:
Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...

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
 
E

Eric Sosman

Harish wrote On 05/07/07 14:16,:
Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...

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

Lulu58e2

Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...

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>
 
R

Richard Reynolds

Harish said:
Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...

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?
 
T

Torain

Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...

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
 
M

Mike Schilling

Harish said:
Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...

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

Mike Schilling

Torain said:
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.
 
D

Daniel Pitts

Object o = "Hello, World";
....
String x1 = o.toString();
// or
String x2 = (String)o;
...

which one will perform better? typecast or toString?

String.valueOf(o) performs better than either of those, because it
should never throw an exception!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top