Which is more efficient?

S

suneet.taparia

Hi

Can any one tell me which is better among the following piece of codes

String abc = (String) obj

or

String abc = obj.toString();

Thanks in advance
Suneet..
 
T

Thomas Weidenfeller

Can any one tell me which is better among the following piece of codes

Well, one works, one can only work in one very special case. I would
recommend you use the one that works. Which is that? Well, why don't you
try? You could learn something along the way.

/Thomas
 
S

Simon

Can any one tell me which is better among the following piece of codes

String abc = (String) obj

or

String abc = obj.toString();

The question is not which one is more efficient, but rather which one is more
effective. If obj actually is a string, abc will refer to the same String
instance in both cases, since obj.toString() merely returns itself. However, if
obj is not a String, the first piece of code will throw an exception at runtime
whereas the second will not (unless obj is null) but will return a string
representation of your object which is probably of very little use. If you know
that obj is a String, you should still use the typecast, but not for reasons of
efficiency, but to prevent your program from using a bogus string in case you
were wrong.

Cheers,
Simon
 
R

Robert Klemme

Can any one tell me which is better among the following piece of codes

String abc = (String) obj

or

String abc = obj.toString();

As others have pointed out these two do not have the same semantics. To
make the picture more complete there is also

String abc = String.valueOf( obj );

You need to first determine what obj can be and what you want. Then
choose the implementations with the proper semantics and then you can
think about which of those is the fastest.

Kind regards

robert
 
M

Michael Rauscher

Hi

Can any one tell me which is better among the following piece of codes

String abc = (String) obj

or

String abc = obj.toString();

Besides of what the others have already written:

obj == null: The first one works, the second results in a
NullPointerException.

obj != null: Two more cases:
obj instanceof String ==> the first one is more efficient
otherwise ==> the first one results in a ClassCastException,
only the second one works.

HTH
Michael
 
T

Thomas Weidenfeller

Chris said:
Why be condescending and give an answer that is not in any way helpful?

Did you recently morph, or why aren't you in my killfile?

I consider it helpful to let people do their own discoveries instead of
spoon-feeding them. If you can't stand this, use your newsreader's
features wisely. Oh, you use google groups? Well, then your only options
are to live with it, or go away.

/Thomas
 
C

Chris Uppal

String abc = (String) obj

or

String abc = obj.toString();

I don't think the previous posts have really emphasised the following strongly
enough.

These two bits of code mean /completely/ different things -- there is
no point in considering which is more "efficient" because you are not
comparing like with like (nor even close).

There are no cases where you would ever have to make a choice between the two
expression. If either one is a valid expression of what the code is trying to
do, then the other would be just plain wrong.

You probably know what toString() means, since you will have used it on many
kinds of objects. If you need more help on what casting means (and if the
other posts don't make it clear) then you'd better ask again.

-- chris
 
R

Robert Klemme

Why be condescending and give an answer that is not in any way helpful?

Are you referring to my posting? If so: I did not mean to be
condescending - I was just a tad short because I'm short in time. Apart
form that, I think the answer was helpful.

Regards

robert
 
C

Christopher Benson-Manica

Are you referring to my posting? If so: I did not mean to be
condescending - I was just a tad short because I'm short in time. Apart
form that, I think the answer was helpful.

Mr. Brat was in fact referring to a post by Thomas Wiedenfeller, but
his point remains dubious.
 
R

richardsosborn

Thomas said:
Well, one works, one can only work in one very special case. I would
recommend you use the one that works. Which is that? Well, why don't you
try? You could learn something along the way.

/Thomas


i agree with the following post.
for those of us who frequent regularly, this topic or ones similar
are posted every other week, with effort on the part of the poster.
it would be nice if folks made some effort to solve their problems
as well as ask. it would make it easier for us to assist also.
 
S

Simon

i agree with the following post.

I don't (if you mean following==preceding).
for those of us who frequent regularly, this topic or ones similar
are posted every other week, with effort on the part of the poster.

And what do you believe is the reason for that?

- If someone believes toString() could be something similar to a typecast, he
will have a hard time finding this misunderstanding resolved in a book.
Typecasts and the toString() method will be treated in separate chapters.

- It is not obvious what good keywords for a web search are.

- If the obj in the OP's code is actually a String he will be unable to see the
difference, simply because in that case there *is* no difference. Both pieces of
code will assign precisely the same reference to abc. To see the difference, it
would be necessary to replace obj by an instance of some other class which is
not an obvious idea for a beginner since it takes you farther away from what you
want to achieve.
it would be nice if folks made some effort to solve their problems
as well as ask. it would make it easier for us to assist also.

I don't see any evidence for the fact that the OP didn't make any effort (though
I don't see evidence for the converse either). Even if we agree that there are
unnecessary posts in these groups, moaning about this fact won't stop the next
from posting a similar question.

The only point I see is that this question belongs to cljh instead of cljp.

Cheers,
Simon
 
A

adwords

The toString call is by far the best, as every method can respond to
the toString method, even if they are not a String.

On the other hand, if you cast somethign that is not a String, into a
String, then you're in a world of hurt.

Stay away from casts in Java as much as possible, and you'll avoid the
intolerable ClassCastException.

Cheers!

-Cameron McKenzie www.pulpjava.com www.examscam.com www.scja.com

Check out my new SCJA certification guides and mock exam questions at
www.scja.com
 
R

Robert Klemme

The toString call is by far the best, as every method can respond to
the toString method, even if they are not a String.

You are confusing "method" and "object" here. Also, "null" definitively
does not respond to toString() - at least not in the way that you
probably meant.
On the other hand, if you cast somethign that is not a String, into a
String, then you're in a world of hurt.

Stay away from casts in Java as much as possible, and you'll avoid the
intolerable ClassCastException.

I am sorry, but this advice is nonsense for Java prior to 1.5. Even
with the current version you likely need a cast once in a while.
Demonizing does not help.

Regards

robert
 
C

Chris Uppal

The toString call is by far the best, as every method can respond to
the toString method, even if they are not a String.

On the other hand, if you cast somethign that is not a String, into a
String, then you're in a world of hurt.

This is extremely poor advice.

This is Usenet, where poor advice is hardly unusual, so I wouldn't have
mentioned it, except that such advice is not a good advertisement for:
Check out my new SCJA certification guides and mock exam questions at
www.scja.com

-- chris
 

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
474,266
Messages
2,571,085
Members
48,773
Latest member
Kaybee

Latest Threads

Top