Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Which is more efficient?

18 views
Skip to first unread message

suneet....@gmail.com

unread,
Oct 12, 2006, 3:37:02 AM10/12/06
to
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..

Thomas Weidenfeller

unread,
Oct 12, 2006, 3:44:23 AM10/12/06
to
suneet....@gmail.com wrote:
> 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
--
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Simon

unread,
Oct 12, 2006, 3:57:56 AM10/12/06
to
suneet....@gmail.com wrote:
> 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

Robert Klemme

unread,
Oct 12, 2006, 4:05:58 AM10/12/06
to
On 12.10.2006 09:37, suneet....@gmail.com wrote:
> 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

Message has been deleted

Michael Rauscher

unread,
Oct 12, 2006, 5:32:51 AM10/12/06
to
suneet....@gmail.com schrieb:

> 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

Thomas Weidenfeller

unread,
Oct 12, 2006, 5:35:42 AM10/12/06
to
Chris Brat wrote:
> 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.

Chris Uppal

unread,
Oct 12, 2006, 5:46:45 AM10/12/06
to
suneet....@gmail.com wrote:

> 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


Message has been deleted

Robert Klemme

unread,
Oct 12, 2006, 6:29:31 AM10/12/06
to
On 12.10.2006 10:49, Chris Brat wrote:
> 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

Christopher Benson-Manica

unread,
Oct 12, 2006, 2:16:22 PM10/12/06
to
Robert Klemme <short...@googlemail.com> wrote:

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

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

richard...@gmail.com

unread,
Oct 12, 2006, 6:11:36 PM10/12/06
to


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.

Simon

unread,
Oct 13, 2006, 3:07:51 AM10/13/06
to
richard...@gmail.com schrieb:

> Thomas Weidenfeller wrote:
>> suneet....@gmail.com wrote:
>>> 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
>> --
>> The comp.lang.java.gui FAQ:
>> http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
>> ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
>
>
> 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

adw...@pulpjava.com

unread,
Oct 13, 2006, 9:18:21 AM10/13/06
to
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

Robert Klemme

unread,
Oct 13, 2006, 9:45:35 AM10/13/06
to
On 13.10.2006 15:18, adw...@pulpjava.com wrote:
> 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

Chris Uppal

unread,
Oct 14, 2006, 6:06:56 AM10/14/06
to
adw...@pulpjava.com wrote:
> 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

0 new messages