Dumb instanceof question  
Author Message
ptomblin





PostPosted: 2003-7-12 3:20:00 Top

java-programmer, Dumb instanceof question In a previous article, email***@***.com said:
>Is instanceof true for a superclass?
>
>So, if I have a Class MyClass and also MyClassSubClass, and I have an
>instance of MyClassSubClass mcsc, is (mcsc instanceof MyClass) true?

Yes.



--
Paul Tomblin <email***@***.com>, not speaking for anybody
"The only thing that interferes with my learning is my education."
-- Albert Einstein.
 
Mike Schilling





PostPosted: 2003-7-12 3:48:00 Top

java-programmer >> Dumb instanceof question
"Paul Tomblin" <email***@***.com> wrote in message
news:ben2ln$drf$email***@***.com...
> In a previous article, email***@***.com said:
> >Is instanceof true for a superclass?
> >
> >So, if I have a Class MyClass and also MyClassSubClass, and I have an
> >instance of MyClassSubClass mcsc, is (mcsc instanceof MyClass) true?
>
> Yes.

Likewise, if MyClassSubClass implements MyInterface, then (mcsc instanceof
MyInterface ) is also true.


 
Chris Rehm





PostPosted: 2003-7-12 4:51:00 Top

java-programmer >> Dumb instanceof question Mike Schilling wrote:
> "Paul Tomblin" <email***@***.com> wrote in message
> news:ben2ln$drf$email***@***.com...
>>>So, if I have a Class MyClass and also MyClassSubClass, and I have an
>>>instance of MyClassSubClass mcsc, is (mcsc instanceof MyClass) true?
>>Yes.
> Likewise, if MyClassSubClass implements MyInterface, then (mcsc instanceof
> MyInterface ) is also true.

Thanks guys. When I first read instanceof, I didn't understand. Now I
have an embarrassing amount of really crappy code to go clean up.

--
Chris Rehm
email***@***.com

Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
 
Marco Schmidt





PostPosted: 2003-7-12 5:08:00 Top

java-programmer >> Dumb instanceof question Chris Rehm:

>Thanks guys. When I first read instanceof, I didn't understand. Now I
>have an embarrassing amount of really crappy code to go clean up.

If you use instanceof very often there is a good chance you could
improve the design of your program. Is there any typical situation
where you would check objects with instanceof?

Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
 
 
Chris Rehm





PostPosted: 2003-7-12 5:56:00 Top

java-programmer >> Dumb instanceof question Marco Schmidt wrote:
> If you use instanceof very often there is a good chance you could
> improve the design of your program. Is there any typical situation
> where you would check objects with instanceof?

Well, I guess I can risk a little humiliation to gain a little
education, so here goes:

I have a "primary" Class, with two branches of sub-classes:

MyClassBase

MyClassBaseSquare MyClassBaseNSquare
MyClassBaseSquareX MyClassBaseNSquareA

They are all related objects (um, I suppose you'd guess that from the
superclass) and I have various reasons for loading up Vectors with lots
of MyClass objects. But NSquare objects have different methods that need
to be run.

A specific example is that I have a Vector holding lots of MyClassBase.
I want to create a descriptive list of only the Square, none of the
NSquare. Plus, I need to run a method on all the NSquares, that does not
exist on the Squares.

So, when I get there I have been using a lot of
getClass().getName().indexOf("NSquare)<0 to figure out if I can cast.

I think I might have been better off if I'd done a better job of
designing the classes before I started, but I figured out too much of
what was going to happen after I got going.

>
> Regards,
> Marco

--
Chris Rehm
email***@***.com

Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
 
ptomblin





PostPosted: 2003-7-12 6:59:00 Top

java-programmer >> Dumb instanceof question In a previous article, email***@***.com said:
>I think I might have been better off if I'd done a better job of
>designing the classes before I started, but I figured out too much of
>what was going to happen after I got going.

You got that right. Instead of doing "instanceof", deciding if you can
cast, and then calling the method, make a no-op method in the base class,
and call it on all of the instances.


--
Paul Tomblin <email***@***.com>, not speaking for anybody
"He passed away during an important civic function held in his honor when the
platform upon which he was standing collapsed." "I thought he was hanged?"
"That's what I said, isn't it?"
 
 
VisionSet





PostPosted: 2003-7-12 8:21:00 Top

java-programmer >> Dumb instanceof question "Chris Rehm" <email***@***.com> wrote in message
news:benbpv$nf4$email***@***.com...
>I think I might have been better off if I'd done a better job of
>designing the classes before I started, but I figured out too much of
>what was going to happen after I got going.

Often the way I find.

If we knew the language like the back of our (my) hand then there'd be less
excuse, but it always seems to be a many (one heck of a lot) tangents/fact
finding process. Design>try>learn language>learn pattern>try>redesign etc.

Then when you've gone full circle, 1.4 comes out with a lovely neat package
that cuts out a month of previous learning, frustrating business sometimes!


 
 
Sudsy





PostPosted: 2003-7-12 15:18:00 Top

java-programmer >> Dumb instanceof question Chris Rehm wrote:
> Marco Schmidt wrote:
>
>> If you use instanceof very often there is a good chance you could
>> improve the design of your program. Is there any typical situation
>> where you would check objects with instanceof?

Actually, I use instanceof for validity checking and also flow-of-
control when using a Struts Action which can be fed by multiple
ActionForms.
If I have a daemon which has to respond to multiple input types,
typically through an ObjectInputStream chained to a Socket then
the following code:

if( obj instanceof x ) {
X x = (X) obj;
// process
}
if( obj instanceof Y ) {
Y y = (Y) obj;
// process
}

is preferable to:

try {
X x = (X) obj;
// process
}
catch( ClassCastException e ) {
}
try {
Y y = (Y) obj;
// process
}
catch( ClassCastException e ) {
}

A common mistake seen is newbie code is to even nest these
tests! So instanceof can be your friend in many situations,
especially when you're using an object interface which offers
no guarantees of the class.

 
 
Thomas G. Marshall





PostPosted: 2003-7-13 0:16:00 Top

java-programmer >> Dumb instanceof question In news:beo7q1$qku$email***@***.com,
Chris Rehm <email***@***.com> spoke unto us:
> VisionSet wrote:
>> Then when you've gone full circle, 1.4 comes out with a lovely neat
>> package that cuts out a month of previous learning, frustrating
>> business sometimes!
>
> Frustrating sometimes? Yes. But since I started programming back in
> the 70s, I've never had as much fun as I do with Java. I don't know
> why, but
> I just find it _fun_ to code in this language.
>
> It's like the language embodies all the things I've always tried to do
> in my coding.

Yes, but I believe that what you like is the language, and not the libraries
(package set), which is what VisionSet was refering to.

And I totally agree: Java has done an amazingly number of things right.

And what I find very interesting as well: in comp.object, where people are
interested in language non-specific OO issues, the pseudo code they end up
posting as examples almost always looks exactly like java. FWIW.



 
 
Thomas G. Marshall





PostPosted: 2003-7-13 0:24:00 Top

java-programmer >> Dumb instanceof question In news:beofsk$ol5$email***@***.com,
Chris Rehm <email***@***.com> spoke unto us:
> Sudsy wrote:
>> A common mistake seen is newbie code is to even nest these
>> tests! So instanceof can be your friend in many situations,
>> especially when you're using an object interface which offers
>> no guarantees of the class.
>
> I'm in full agreement that instanceof is orders of magnitude faster
> than try catch around a cast. But when I'm using instanceof I sort of
> feel (usually) like I must have designed something wrong if I have to
> know what kind of class I'm dealing with right here in my code.
>
> It's like a goto in C. In all the time I wrote C code, I never used a
> goto. Whenever I am thinking I want to branch like that, I am pretty
> sure I must have not conceptualized my code right in the first place.
> That's how I feel when I need to check what kind of object I have.
>
> Does that make sense? That doesn't mean I know I'm right about it,
> just that's the area where I feel self conscious.

You are half right, and that half is right on the money. :) I use
instanceof, but c.a.r.e.f.u.l.l.y. In the case of the prior poster example,
he is using instance of as a receiving mechanism for a form of dispatch,
which is fine in my opinion.

The problem with instanceof is that it can be grossly overused to give a
java programmer a way around having to implement proper OO design. This is
the source of many an thread in comp.object with subjects of the form "Is
RTTI really all that bad?"

OO purists almost always want RTTI abolished completely. But I, as you,
liken that to the anti-goto nazi's. I believe that the proper mindset WOULD
be parallel to java's named-break: allow downward goto's only and only out
of code blocks. I believe that instanceof is a little too overused, but I
don't just don't know of a way of providing such functionality without
allowing the abuse.

Thomas


 
 
Jon A. Cruz





PostPosted: 2003-7-13 0:42:00 Top

java-programmer >> Dumb instanceof question Sudsy wrote:
> A common mistake seen is newbie code is to even nest these
> tests! So instanceof can be your friend in many situations,
> especially when you're using an object interface which offers
> no guarantees of the class.
>

However...

Remember that null is instanceOf everything.

:-D

So before anything else, check for null.

 
 
Harald Hein





PostPosted: 2003-7-13 3:53:00 Top

java-programmer >> Dumb instanceof question "Jon A. Cruz" wrote:

> Remember that null is instanceOf everything.

It is the other way around. From the JLS:

| At run time, the result of the instanceof operator is true if the
| value of the RelationalExpression is not null and the reference
^^^^^^^^^^^
| could be cast (?5.16) to the ReferenceType without raising a
| ClassCastException. Otherwise the result is false.

And every Java compiler I have ever used was compliant.

> So before anything else, check for null.

No, instanceof includes a check for null.

HH
 
 
Chris Rehm





PostPosted: 2003-7-13 5:31:00 Top

java-programmer >> Dumb instanceof question Thomas G. Marshall wrote:
> Yes, but I believe that what you like is the language, and not the libraries
> (package set), which is what VisionSet was refering to.

I think you are right. OO coding just "feels" right in Java. Now, I'm
sounding awful weird. Honest, I have a wife and a life! I'm not living
in my parent's basement!

> And I totally agree: Java has done an amazingly number of things right.
>
> And what I find very interesting as well: in comp.object, where people are
> interested in language non-specific OO issues, the pseudo code they end up
> posting as examples almost always looks exactly like java. FWIW.

I think I might want to add the group to my subscriptions.

--
Chris Rehm
email***@***.com

Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
 
Chris Rehm





PostPosted: 2003-7-13 5:38:00 Top

java-programmer >> Dumb instanceof question Thomas G. Marshall wrote:
> of code blocks. I believe that instanceof is a little too overused, but I
> don't just don't know of a way of providing such functionality without
> allowing the abuse.

Well, absolutely. It is a tool, and as you've pointed out there are
valid places for it. But there will always be programmers who misuse
things. That's just people. There's a million reasons for making a
mistake, from straight laziness to poor judgment.

I'm just afraid that I've been guilty of the latter lately. But I think
the more Java I code, the better I get at thinking right with it.

> Thomas

--
Chris Rehm
email***@***.com

Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
 
Chris Rehm





PostPosted: 2003-7-13 5:42:00 Top

java-programmer >> Dumb instanceof question Harald Hein wrote:
> | At run time, the result of the instanceof operator is true if the
> | value of the RelationalExpression is not null and the reference
> ^^^^^^^^^^^
> | could be cast (?5.16) to the ReferenceType without raising a
> | ClassCastException. Otherwise the result is false.

Where is that from? What's JLS? Is that Java Language Specs? Is that on
line? Because that little paragraph above just totally answers all I
needed to know about instanceof. Very clear.

> HH

--
Chris Rehm
email***@***.com

Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
 
Tor Iver Wilhelmsen





PostPosted: 2003-7-13 16:12:00 Top

java-programmer >> Dumb instanceof question Chris Rehm <email***@***.com> writes:

> Where is that from? What's JLS? Is that Java Language Specs? Is that
> on line? Because that little paragraph above just totally answers all
> I needed to know about instanceof. Very clear.

http://java.sun.com/docs/books/jls/index.html

 
 
Chris Rehm





PostPosted: 2003-7-14 3:40:00 Top

java-programmer >> Dumb instanceof question Thank you. I am embarrassed and ashamed that I missed that. With all the
java.sun.com bookmarks I've got for docs on their site, I didn't find
the LANGUAGE SPEC?! Holy freakin' moly.

Tor Iver Wilhelmsen wrote:

> Chris Rehm <email***@***.com> writes:
>
>
>>Where is that from? What's JLS? Is that Java Language Specs? Is that
>>on line? Because that little paragraph above just totally answers all
>>I needed to know about instanceof. Very clear.
>
>
> http://java.sun.com/docs/books/jls/index.html
>

--
Chris Rehm
email***@***.com

Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
 
Chris Rehm





PostPosted: 2003-7-14 8:29:00 Top

java-programmer >> Dumb instanceof question To clarify, and to amuse those whom I've annoyed with silly language
questions:

For some reason I never found the Language Spec at Sun, or if I'd found
it I glossed over the page thinking it an ad for books. I guess I
expected to find some vanilla looking document.

So, up till now I've been using a downloaded copy of the Java Tutorial
for my Language Reference. Whenever I needed to figure out some piece of
the language, I'd hunt through there to find a reference to what I
wanted to know. Somehow, this just doesn't have the clarity and accuracy
of just Reading The Fine (I'm a religious guy!) Manual.

In my defense, in all the time I've been learning languages, I've never
before had the massive amount of information available on line as there
is for Java. Old habits die hard, and it is tough learning that I can
find the reference by just clicking on it.

Thanks again for all the help.

Chris Rehm wrote:
> Thank you. I am embarrassed and ashamed that I missed that. With all the
> java.sun.com bookmarks I've got for docs on their site, I didn't find
> the LANGUAGE SPEC?! Holy freakin' moly.
>
> Tor Iver Wilhelmsen wrote:
>
>> Chris Rehm <email***@***.com> writes:
>>
>>
>>> Where is that from? What's JLS? Is that Java Language Specs? Is that
>>> on line? Because that little paragraph above just totally answers all
>>> I needed to know about instanceof. Very clear.
>>
>>
>>
>> http://java.sun.com/docs/books/jls/index.html
>>
>

--
Chris Rehm
email***@***.com

Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
 
Thomas G. Marshall





PostPosted: 2003-7-15 1:03:00 Top

java-programmer >> Dumb instanceof question In news:email***@***.com,
Harald Hein <email***@***.com> spoke unto us:
> "Jon A. Cruz" wrote:
>
>> Remember that null is instanceOf everything.
>
> It is the other way around. From the JLS:
>
>> At run time, the result of the instanceof operator is true if the
>> value of the RelationalExpression is not null and the reference
> ^^^^^^^^^^^
>> could be cast (?5.16) to the ReferenceType without raising a
>> ClassCastException. Otherwise the result is false.

WHOA dude. Don't quote something by placing a ">" or the like in front of
it---it reads as if Jon Cruz wrote it.


 
 
Harald Hein





PostPosted: 2003-7-16 2:16:00 Top

java-programmer >> Dumb instanceof question "Thomas G. Marshall" wrote:

> WHOA dude. Don't quote something by placing a ">" or the like in
> front of it---it reads as if Jon Cruz wrote it.

Well dude, then get your act together. Jon's statements were marked
with "> ", while the JLS quote was marked with "|". And the
beginning of the JLS quote reads "From the JLS:".

http://groups.google.com/groups?selm=Xns93B6DEB712447hhtoken%40194.97
.5.17

If you can't get the difference, then I really don't want to see
your source code.

You changed the "|" in your post to ">" for what reason exactly? To
start an argument? Dude, if you want to start an argument, please
come up with something more original.

HH