Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Which is more efficient?

Thread view: 
suneet.taparia@gmail.com - 12 Oct 2006 08:37 GMT
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 - 12 Oct 2006 08:44 GMT
> 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
Signature

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

Chris Brat - 12 Oct 2006 09:49 GMT
Why be condescending and give an answer that is not in any way helpful?
Thomas Weidenfeller - 12 Oct 2006 10:35 GMT
> 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
Signature

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

Chris Brat - 12 Oct 2006 10:46 GMT
Robert Klemme - 12 Oct 2006 11:29 GMT
> 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 - 12 Oct 2006 19:16 GMT
> > 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.

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

Signature

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.

richardsosborn@gmail.com - 12 Oct 2006 23:11 GMT
> > Can any one tell me which is better among the following piece of codes
>
[quoted text clipped - 7 lines]
> 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.
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 - 13 Oct 2006 08:07 GMT
richardsosborn@gmail.com schrieb:
>>> 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
[quoted text clipped - 8 lines]
>
> 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
Simon - 12 Oct 2006 08:57 GMT
> Can any one tell me which is better among the following piece of codes
>
[quoted text clipped - 3 lines]
>
> 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 - 12 Oct 2006 09:05 GMT
> Can any one tell me which is better among the following piece of codes
>
[quoted text clipped - 3 lines]
>
> 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
Michael Rauscher - 12 Oct 2006 10:31 GMT
suneet.taparia@gmail.com schrieb:
> Hi
>
[quoted text clipped - 5 lines]
>
> 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
Chris Uppal - 12 Oct 2006 10:46 GMT
> 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
adwords@pulpjava.com - 13 Oct 2006 14:18 GMT
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

> Hi
>
[quoted text clipped - 8 lines]
> Thanks in advance
> Suneet..
Robert Klemme - 13 Oct 2006 14:45 GMT
> 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 - 14 Oct 2006 11:06 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.