Re: Dumb instanceof question

Discussion in 'Java' started by Paul Tomblin, Jul 11, 2003.

  1. Paul Tomblin

    Paul Tomblin Guest

    In a previous article, 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 <>, not speaking for anybody
    "The only thing that interferes with my learning is my education."
    -- Albert Einstein.
     
    Paul Tomblin, Jul 11, 2003
    #1
    1. Advertising

  2. "Paul Tomblin" <> wrote in message
    news:ben2ln$drf$...
    > In a previous article, 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.
     
    Mike Schilling, Jul 11, 2003
    #2
    1. Advertising

  3. Paul Tomblin

    Chris Rehm Guest

    Mike Schilling wrote:
    > "Paul Tomblin" <> wrote in message
    > news:ben2ln$drf$...
    >>>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


    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, Jul 11, 2003
    #3
  4. 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
     
    Marco Schmidt, Jul 11, 2003
    #4
  5. Paul Tomblin

    Chris Rehm Guest

    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


    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, Jul 11, 2003
    #5
  6. Paul Tomblin

    Paul Tomblin Guest

    In a previous article, 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 <>, 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?"
     
    Paul Tomblin, Jul 11, 2003
    #6
  7. Paul Tomblin

    VisionSet Guest

    "Chris Rehm" <> wrote in message
    news:benbpv$nf4$...
    >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!

    --
    Mike W
     
    VisionSet, Jul 12, 2003
    #7
  8. Paul Tomblin

    Sudsy Guest

    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.
     
    Sudsy, Jul 12, 2003
    #8
  9. In news:beo7q1$qku$,
    Chris Rehm <> 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, Jul 12, 2003
    #9
  10. In news:beofsk$ol5$,
    Chris Rehm <> 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
     
    Thomas G. Marshall, Jul 12, 2003
    #10
  11. Paul Tomblin

    Jon A. Cruz Guest

    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.
     
    Jon A. Cruz, Jul 12, 2003
    #11
  12. Paul Tomblin

    Harald Hein Guest

    "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 (§15.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
     
    Harald Hein, Jul 12, 2003
    #12
  13. Paul Tomblin

    Chris Rehm Guest

    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


    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, Jul 12, 2003
    #13
  14. Paul Tomblin

    Chris Rehm Guest

    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


    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, Jul 12, 2003
    #14
  15. Paul Tomblin

    Chris Rehm Guest

    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 (§15.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


    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, Jul 12, 2003
    #15
  16. Chris Rehm <> 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
     
    Tor Iver Wilhelmsen, Jul 13, 2003
    #16
  17. Paul Tomblin

    Chris Rehm Guest

    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 <> 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


    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, Jul 13, 2003
    #17
  18. Paul Tomblin

    Chris Rehm Guest

    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 <> 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


    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, Jul 14, 2003
    #18
  19. In news:Xns93B6DEB712447hhtoken@194.97.5.17,
    Harald Hein <> 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 (§15.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.
     
    Thomas G. Marshall, Jul 14, 2003
    #19
  20. Paul Tomblin

    Harald Hein Guest

    "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@194.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
     
    Harald Hein, Jul 15, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dotty

    instanceof question

    Dotty, Feb 16, 2005, in forum: Java
    Replies:
    2
    Views:
    467
    dar7yl
    Feb 16, 2005
  2. WillieLWZ
    Replies:
    8
    Views:
    445
    WillieLWZ
    Feb 23, 2006
  3. Replies:
    14
    Views:
    2,818
    Chris Uppal
    May 2, 2006
  4. Replies:
    21
    Views:
    21,574
  5. Jerry C.
    Replies:
    8
    Views:
    245
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page