× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Price, Chris wrote:

>Is it "correct" to use the toString method on the Object, rather than
>casting it to String?
>
Well, you are going to get a String back from the toString() method no
matter what kind of object it really is. So you could be fooled if you
got an AddressBook object there. But if what you want is your method to
deal with the String representation of whatever gets sent to it, then
toString() will be your choice.

On the other hand, if what you want is your method to only work with
Strings, you better try and catch a cast to String to make sure you
aren't running against some wrong object passed to it.

>Which is more efficient?
>
I'm pretty sure toString() would be more efficient because it just runs
that method but casting requires a type check. But I'm just
guesstimating here.

But now, you have said you already "know" the object being referred to
is a String and you are just referring through an interface that doesn't
give you a String object. But the reason for that is the interface
includes the ability to access non-String objects with your same method.
If you are writing around that, you are defeating the OO intent of the
interface.

Instead, you just need to decide how you want your method to behave and
code appropriately.

>Chris.
>

--
Chris Rehm
javadisciple@earthlink.net

Beloved, let us love one another: for love is of God; and every one
that loveth is born of God, and knoweth God. 1 John 4:7





As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.